| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| node-npmtest-strider/ | 100% | (153 / 153) | 100% | (126 / 126) | 100% | (28 / 28) | 100% | (153 / 153) | |
| node-npmtest-strider/node_modules/strider-cli/lib/ | 27.27% | (6 / 22) | 0% | (0 / 4) | 16.67% | (1 / 6) | 28.57% | (6 / 21) | |
| node-npmtest-strider/node_modules/strider-cli/lib/plugin_manager/ | 38.24% | (13 / 34) | 100% | (0 / 0) | 13.33% | (2 / 15) | 38.24% | (13 / 34) | |
| node-npmtest-strider/node_modules/strider-cli/node_modules/lodash/dist/ | 28.24% | (453 / 1604) | 7% | (88 / 1257) | 9.65% | (22 / 228) | 28.51% | (453 / 1589) | |
| node-npmtest-strider/node_modules/strider-ecosystem-client/ | 65% | (13 / 20) | 0% | (0 / 2) | 20% | (1 / 5) | 68.42% | (13 / 19) | |
| node-npmtest-strider/node_modules/strider-ecosystem-client/node_modules/bluebird/js/main/ | 28.78% | (835 / 2901) | 6.17% | (73 / 1183) | 10.89% | (55 / 505) | 29.76% | (827 / 2779) | |
| node-npmtest-strider/node_modules/strider-extension-loader/ | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| node-npmtest-strider/node_modules/strider-extension-loader/lib/ | 12.37% | (47 / 380) | 0.93% | (2 / 215) | 1.14% | (1 / 88) | 13.62% | (47 / 345) | |
| node-npmtest-strider/node_modules/strider-extension-loader/node_modules/async/lib/ | 16.51% | (89 / 539) | 4.5% | (10 / 222) | 2.22% | (4 / 180) | 16.64% | (89 / 535) | |
| node-npmtest-strider/node_modules/strider-extension-loader/node_modules/methods/ | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| node-npmtest-strider/node_modules/strider-extension-loader/node_modules/underscore/ | 21.79% | (151 / 693) | 3.42% | (15 / 439) | 9.93% | (15 / 151) | 24.71% | (150 / 607) | |
| node-npmtest-strider/node_modules/strider-mailer/ | 27.59% | (16 / 58) | 12.9% | (4 / 31) | 14.29% | (1 / 7) | 27.59% | (16 / 58) | |
| node-npmtest-strider/node_modules/strider-mailer/node_modules/everypaas/ | 45.05% | (41 / 91) | 27.59% | (16 / 58) | 63.64% | (7 / 11) | 45.56% | (41 / 90) | |
| node-npmtest-strider/node_modules/strider/ | 33.82% | (23 / 68) | 0% | (0 / 18) | 0% | (0 / 12) | 34.33% | (23 / 67) | |
| node-npmtest-strider/node_modules/strider/apidocs/ | 100% | (2 / 2) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (2 / 2) | |
| node-npmtest-strider/node_modules/strider/apidocs/locales/ | 45.16% | (14 / 31) | 0% | (0 / 12) | 0% | (0 / 3) | 45.16% | (14 / 31) | |
| node-npmtest-strider/node_modules/strider/apidocs/utils/ | 3.8% | (10 / 263) | 0% | (0 / 82) | 0% | (0 / 48) | 3.83% | (10 / 261) | |
| node-npmtest-strider/node_modules/strider/client/ | 3.85% | (1 / 26) | 100% | (0 / 0) | 100% | (0 / 0) | 3.85% | (1 / 26) | |
| node-npmtest-strider/node_modules/strider/client/utils/ | 11.82% | (24 / 203) | 0% | (0 / 82) | 0% | (0 / 36) | 12.7% | (24 / 189) | |
| node-npmtest-strider/node_modules/strider/lib/ | 25.11% | (287 / 1143) | 6.62% | (29 / 438) | 8.09% | (14 / 173) | 26.13% | (283 / 1083) | |
| node-npmtest-strider/node_modules/strider/lib/models/ | 27.09% | (55 / 203) | 0.94% | (1 / 106) | 0% | (0 / 36) | 27.09% | (55 / 203) | |
| node-npmtest-strider/node_modules/strider/lib/models/upgrade/ | 13.99% | (34 / 243) | 0% | (0 / 108) | 0% | (0 / 50) | 15.53% | (34 / 219) | |
| node-npmtest-strider/node_modules/strider/lib/routes/ | 24.08% | (46 / 191) | 0% | (0 / 51) | 0% | (0 / 43) | 24.47% | (46 / 188) | |
| node-npmtest-strider/node_modules/strider/lib/routes/admin/ | 21.65% | (21 / 97) | 0% | (0 / 26) | 0% | (0 / 19) | 22.58% | (21 / 93) | |
| node-npmtest-strider/node_modules/strider/lib/routes/admin/plugins/ | 37.5% | (18 / 48) | 0% | (0 / 12) | 10% | (1 / 10) | 39.13% | (18 / 46) | |
| node-npmtest-strider/node_modules/strider/lib/routes/api/ | 20.97% | (69 / 329) | 0% | (0 / 148) | 0% | (0 / 55) | 21.97% | (69 / 314) | |
| node-npmtest-strider/node_modules/strider/lib/routes/api/admin/ | 36.84% | (14 / 38) | 0% | (0 / 6) | 0% | (0 / 8) | 36.84% | (14 / 38) | |
| node-npmtest-strider/node_modules/strider/lib/routes/collaborators/ | 25.56% | (23 / 90) | 0% | (0 / 34) | 0% | (0 / 18) | 28.4% | (23 / 81) | |
| node-npmtest-strider/node_modules/strider/lib/routes/jobs/ | 18% | (18 / 100) | 0% | (0 / 45) | 0% | (0 / 19) | 19.15% | (18 / 94) | |
| node-npmtest-strider/node_modules/strider/lib/utils/ | 15.69% | (48 / 306) | 0% | (0 / 136) | 0% | (0 / 60) | 17.78% | (48 / 270) | |
| node-npmtest-strider/node_modules/strider/public/libs/ | 4.29% | (30 / 699) | 0% | (0 / 390) | 0% | (0 / 172) | 4.29% | (30 / 699) |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| example.js | 100% | (83 / 83) | 100% | (73 / 73) | 100% | (12 / 12) | 100% | (83 / 83) | |
| lib.npmtest_strider.js | 100% | (16 / 16) | 100% | (14 / 14) | 100% | (3 / 3) | 100% | (16 / 16) | |
| test.js | 100% | (54 / 54) | 100% | (39 / 39) | 100% | (13 / 13) | 100% | (54 / 54) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | 2 2 2 2 2 2 2 1 2 2 2 2 1 2 2 2 2 2 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 2 3 3 3 3 1 3 3 3 1 3 1 1 1 1 1 1 1 1 1 1 1 1 6 6 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /*
example.js
quickstart example
instruction
1. save this script as example.js
2. run the shell command:
$ npm install npmtest-strider && PORT=8081 node example.js
3. play with the browser-demo on http://127.0.0.1:8081
*/
/* istanbul instrument in package npmtest_strider */
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 96,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var local;
// run shared js-env code - pre-init
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
// init global
local.global = local.modeJs === 'browser'
? window
: global;
// init utility2_rollup
local = local.global.utility2_rollup || (local.modeJs === 'browser'
? local.global.utility2_npmtest_strider
: global.utility2_moduleExports);
// export local
local.global.local = local;
}());
switch (local.modeJs) {
// post-init
// run browser js-env code - post-init
/* istanbul ignore next */
case 'browser':
local.testRunBrowser = function (event) {
Eif (!event || (event &&
event.currentTarget &&
event.currentTarget.className &&
event.currentTarget.className.includes &&
event.currentTarget.className.includes('onreset'))) {
// reset output
Array.from(
document.querySelectorAll('body > .resettable')
).forEach(function (element) {
switch (element.tagName) {
case 'INPUT':
case 'TEXTAREA':
element.value = '';
break;
default:
element.textContent = '';
}
});
}
switch (event && event.currentTarget && event.currentTarget.id) {
case 'testRunButton1':
// show tests
Eif (document.querySelector('#testReportDiv1').style.display === 'none') {
document.querySelector('#testReportDiv1').style.display = 'block';
document.querySelector('#testRunButton1').textContent =
'hide internal test';
local.modeTest = true;
local.testRunDefault(local);
// hide tests
} else {
document.querySelector('#testReportDiv1').style.display = 'none';
document.querySelector('#testRunButton1').textContent = 'run internal test';
}
break;
// custom-case
default:
break;
}
Iif (document.querySelector('#inputTextareaEval1') && (!event || (event &&
event.currentTarget &&
event.currentTarget.className &&
event.currentTarget.className.includes &&
event.currentTarget.className.includes('oneval')))) {
// try to eval input-code
try {
/*jslint evil: true*/
eval(document.querySelector('#inputTextareaEval1').value);
} catch (errorCaught) {
console.error(errorCaught);
}
}
};
// log stderr and stdout to #outputTextareaStdout1
['error', 'log'].forEach(function (key) {
console[key + '_original'] = console[key];
console[key] = function () {
var element;
console[key + '_original'].apply(console, arguments);
element = document.querySelector('#outputTextareaStdout1');
Iif (!element) {
return;
}
// append text to #outputTextareaStdout1
element.value += Array.from(arguments).map(function (arg) {
return typeof arg === 'string'
? arg
: JSON.stringify(arg, null, 4);
}).join(' ') + '\n';
// scroll textarea to bottom
element.scrollTop = element.scrollHeight;
};
});
// init event-handling
['change', 'click', 'keyup'].forEach(function (event) {
Array.from(document.querySelectorAll('.on' + event)).forEach(function (element) {
element.addEventListener(event, local.testRunBrowser);
});
});
// run tests
local.testRunBrowser();
break;
// run node js-env code - post-init
/* istanbul ignore next */
case 'node':
// export local
module.exports = local;
// require modules
local.fs = require('fs');
local.http = require('http');
local.url = require('url');
// init assets
local.assetsDict = local.assetsDict || {};
/* jslint-ignore-begin */
local.assetsDict['/assets.index.template.html'] = '\
<!doctype html>\n\
<html lang="en">\n\
<head>\n\
<meta charset="UTF-8">\n\
<meta name="viewport" content="width=device-width, initial-scale=1">\n\
<title>{{env.npm_package_name}} (v{{env.npm_package_version}})</title>\n\
<style>\n\
/*csslint\n\
box-sizing: false,\n\
universal-selector: false\n\
*/\n\
* {\n\
box-sizing: border-box;\n\
}\n\
body {\n\
background: #dde;\n\
font-family: Arial, Helvetica, sans-serif;\n\
margin: 2rem;\n\
}\n\
body > * {\n\
margin-bottom: 1rem;\n\
}\n\
.utility2FooterDiv {\n\
margin-top: 20px;\n\
text-align: center;\n\
}\n\
</style>\n\
<style>\n\
/*csslint\n\
*/\n\
textarea {\n\
font-family: monospace;\n\
height: 10rem;\n\
width: 100%;\n\
}\n\
textarea[readonly] {\n\
background: #ddd;\n\
}\n\
</style>\n\
</head>\n\
<body>\n\
<!-- utility2-comment\n\
<div id="ajaxProgressDiv1" style="background: #d00; height: 2px; left: 0; margin: 0; padding: 0; position: fixed; top: 0; transition: background 0.5s, width 1.5s; width: 25%;"></div>\n\
utility2-comment -->\n\
<h1>\n\
<!-- utility2-comment\n\
<a\n\
{{#if env.npm_package_homepage}}\n\
href="{{env.npm_package_homepage}}"\n\
{{/if env.npm_package_homepage}}\n\
target="_blank"\n\
>\n\
utility2-comment -->\n\
{{env.npm_package_name}} (v{{env.npm_package_version}})\n\
<!-- utility2-comment\n\
</a>\n\
utility2-comment -->\n\
</h1>\n\
<h3>{{env.npm_package_description}}</h3>\n\
<!-- utility2-comment\n\
<h4><a download href="assets.app.js">download standalone app</a></h4>\n\
<button class="onclick onreset" id="testRunButton1">run internal test</button><br>\n\
<div id="testReportDiv1" style="display: none;"></div>\n\
utility2-comment -->\n\
\n\
\n\
\n\
<label>stderr and stdout</label>\n\
<textarea class="resettable" id="outputTextareaStdout1" readonly></textarea>\n\
<!-- utility2-comment\n\
{{#if isRollup}}\n\
<script src="assets.app.js"></script>\n\
{{#unless isRollup}}\n\
utility2-comment -->\n\
<script src="assets.utility2.rollup.js"></script>\n\
<script src="jsonp.utility2._stateInit?callback=window.utility2._stateInit"></script>\n\
<script src="assets.npmtest_strider.rollup.js"></script>\n\
<script src="assets.example.js"></script>\n\
<script src="assets.test.js"></script>\n\
<!-- utility2-comment\n\
{{/if isRollup}}\n\
utility2-comment -->\n\
<div class="utility2FooterDiv">\n\
[ this app was created with\n\
<a href="https://github.com/kaizhu256/node-utility2" target="_blank">utility2</a>\n\
]\n\
</div>\n\
</body>\n\
</html>\n\
';
/* jslint-ignore-end */
Iif (local.templateRender) {
local.assetsDict['/'] = local.templateRender(
local.assetsDict['/assets.index.template.html'],
{
env: local.objectSetDefault(local.env, {
npm_package_description: 'the greatest app in the world!',
npm_package_name: 'my-app',
npm_package_nameAlias: 'my_app',
npm_package_version: '0.0.1'
})
}
);
} else {
local.assetsDict['/'] = local.assetsDict['/assets.index.template.html']
.replace((/\{\{env\.(\w+?)\}\}/g), function (match0, match1) {
// jslint-hack
String(match0);
switch (match1) {
case 'npm_package_description':
return 'the greatest app in the world!';
case 'npm_package_name':
return 'my-app';
case 'npm_package_nameAlias':
return 'my_app';
case 'npm_package_version':
return '0.0.1';
}
});
}
// run the cli
Eif (local.global.utility2_rollup || module !== require.main) {
break;
}
local.assetsDict['/assets.example.js'] =
local.assetsDict['/assets.example.js'] ||
local.fs.readFileSync(__filename, 'utf8');
// bug-workaround - long $npm_package_buildCustomOrg
/* jslint-ignore-begin */
local.assetsDict['/assets.npmtest_strider.rollup.js'] =
local.assetsDict['/assets.npmtest_strider.rollup.js'] ||
local.fs.readFileSync(
local.npmtest_strider.__dirname + '/lib.npmtest_strider.js',
'utf8'
).replace((/^#!/), '//');
/* jslint-ignore-end */
local.assetsDict['/favicon.ico'] = local.assetsDict['/favicon.ico'] || '';
// if $npm_config_timeout_exit exists,
// then exit this process after $npm_config_timeout_exit ms
if (Number(process.env.npm_config_timeout_exit)) {
setTimeout(process.exit, Number(process.env.npm_config_timeout_exit));
}
// start server
if (local.global.utility2_serverHttp1) {
break;
}
process.env.PORT = process.env.PORT || '8081';
console.error('server starting on port ' + process.env.PORT);
local.http.createServer(function (request, response) {
request.urlParsed = local.url.parse(request.url);
if (local.assetsDict[request.urlParsed.pathname] !== undefined) {
response.end(local.assetsDict[request.urlParsed.pathname]);
return;
}
response.statusCode = 404;
response.end();
}).listen(process.env.PORT);
break;
}
}());
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 2 2 2 2 2 2 2 1 2 2 2 2 1 1 1 1 | /* istanbul instrument in package npmtest_strider */
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 96,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var local;
// run shared js-env code - pre-init
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
// init global
local.global = local.modeJs === 'browser'
? window
: global;
// init utility2_rollup
local = local.global.utility2_rollup || local;
// init lib
local.local = local.npmtest_strider = local;
// init exports
if (local.modeJs === 'browser') {
local.global.utility2_npmtest_strider = local;
} else {
module.exports = local;
module.exports.__dirname = __dirname;
module.exports.module = module;
}
}());
}());
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 2 2 2 2 2 2 2 1 2 2 1 1 1 1 2 2 2 2 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 2 2 1 2 2 1 2 2 1 1 1 1 1 | /* istanbul instrument in package npmtest_strider */
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 96,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var local;
// run shared js-env code - pre-init
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
// init global
local.global = local.modeJs === 'browser'
? window
: global;
switch (local.modeJs) {
// re-init local from window.local
case 'browser':
local = local.global.utility2.objectSetDefault(
local.global.utility2_rollup || local.global.local,
local.global.utility2
);
break;
// re-init local from example.js
case 'node':
local = (local.global.utility2_rollup || require('utility2'))
.requireExampleJsFromReadme();
break;
}
// export local
local.global.local = local;
}());
// run shared js-env code - function
(function () {
return;
}());
switch (local.modeJs) {
// run browser js-env code - function
case 'browser':
break;
// run node js-env code - function
case 'node':
break;
}
// run shared js-env code - post-init
(function () {
return;
}());
switch (local.modeJs) {
// run browser js-env code - post-init
case 'browser':
local.testCase_browser_nullCase = local.testCase_browser_nullCase || function (
options,
onError
) {
/*
* this function will test browsers's null-case handling-behavior-behavior
*/
onError(null, options);
};
// run tests
local.nop(local.modeTest &&
document.querySelector('#testRunButton1') &&
document.querySelector('#testRunButton1').click());
break;
// run node js-env code - post-init
/* istanbul ignore next */
case 'node':
local.testCase_buildApidoc_default = local.testCase_buildApidoc_default || function (
options,
onError
) {
/*
* this function will test buildApidoc's default handling-behavior-behavior
*/
options = { modulePathList: module.paths };
local.buildApidoc(options, onError);
};
local.testCase_buildApp_default = local.testCase_buildApp_default || function (
options,
onError
) {
/*
* this function will test buildApp's default handling-behavior-behavior
*/
local.testCase_buildReadme_default(options, local.onErrorThrow);
local.testCase_buildLib_default(options, local.onErrorThrow);
local.testCase_buildTest_default(options, local.onErrorThrow);
local.testCase_buildCustomOrg_default(options, local.onErrorThrow);
options = [];
local.buildApp(options, onError);
};
local.testCase_buildCustomOrg_default = local.testCase_buildCustomOrg_default ||
function (options, onError) {
/*
* this function will test buildCustomOrg's default handling-behavior
*/
options = {};
local.buildCustomOrg(options, onError);
};
local.testCase_buildLib_default = local.testCase_buildLib_default || function (
options,
onError
) {
/*
* this function will test buildLib's default handling-behavior
*/
options = {};
local.buildLib(options, onError);
};
local.testCase_buildReadme_default = local.testCase_buildReadme_default || function (
options,
onError
) {
/*
* this function will test buildReadme's default handling-behavior-behavior
*/
options = {};
local.buildReadme(options, onError);
};
local.testCase_buildTest_default = local.testCase_buildTest_default || function (
options,
onError
) {
/*
* this function will test buildTest's default handling-behavior
*/
options = {};
local.buildTest(options, onError);
};
local.testCase_webpage_default = local.testCase_webpage_default || function (
options,
onError
) {
/*
* this function will test webpage's default handling-behavior
*/
options = { modeCoverageMerge: true, url: local.serverLocalHost + '?modeTest=1' };
local.browserTest(options, onError);
};
// run test-server
local.testRunServer(local);
break;
}
}());
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| resilient.js | 27.27% | (6 / 22) | 0% | (0 / 4) | 16.67% | (1 / 6) | 28.57% | (6 / 21) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 1 1 1 | 'use strict';
var cluster = require('cluster');
var chokidar = require('chokidar');
var touch = require('touch');
module.exports = function (deps) {
var flag = deps.restartFile();
return {
restart: function () {
touch.sync(flag);
console.log('touched ' + flag);
},
spawn: function (work, noCluster) {
if (noCluster) return work();
if (cluster.isMaster) {
var watcher = chokidar.watch(flag)
cluster.on('online', function(worker) {
console.log(worker.process.pid +' forked');
watcher.removeAllListeners().on('change', function() {
console.log('restart flag touched');
worker.kill();
});
});
cluster.on('exit', function(worker, code, signal) {
console.log(worker.process.pid + ' died', code, signal);
cluster.fork();
});
cluster.fork();
} else {
work();
}
}
};
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 25% | (2 / 8) | 100% | (0 / 0) | 14.29% | (1 / 7) | 25% | (2 / 8) | |
| local_plugins.js | 42.31% | (11 / 26) | 100% | (0 / 0) | 12.5% | (1 / 8) | 42.31% | (11 / 26) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 1 1 | module.exports = function (pluginsPath) {
return {
listLocal: function() {
return require('./list_local_plugins')(pluginsPath)
},
listRemote: function() {
return require('./list_remote_plugins')(pluginsPath)
},
createNew: function() {
return require('./create_new_plugin')(pluginsPath)
},
install: function(name, cb) {
return require('./install_plugin')(pluginsPath)(name, cb)
},
uninstall: function(name, cb) {
return require('./uninstall_plugin')(pluginsPath)(name, cb)
},
upgrade: function(name, cb) {
return require('./upgrade_plugin')(pluginsPath)(name, cb)
}
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 1 1 1 1 1 1 1 1 1 1 1 | module.exports = function(pluginsPath) {
var Loader = require('strider-extension-loader')
var loader = new Loader();
var path = require('path')
var _ = require('lodash')
return {
path: fullPath,
listAll: listAll,
listAllZipped: listAllZipped
}
function zip(plugins) {
var ids = _.pluck(plugins, 'name')
return _.zipObject(ids, plugins)
}
function fullPath() {
return pluginsPath[0]
}
function getVersion(pluginPath) {
return require(path.join(pluginPath, 'package.json')).version
}
function listAll(cb) {
loader.collectExtensions(pluginsPath, function(err) {
var plugins = []
var extensions = loader.extensions;
for (var groupName in extensions) {
var group = extensions[groupName]
for (var pluginName in group) {
var plugin = group[pluginName]
plugins.push({
group: groupName,
name: pluginName,
path: plugin.dir,
version: getVersion(plugin.dir),
title: plugin.title
})
}
}
cb(err, plugins);
})
}
function listAllZipped(cb) {
listAll(function(err, plugins) {
cb(err, zip(plugins))
})
}
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| lodash.js | 28.24% | (453 / 1604) | 7% | (88 / 1257) | 9.65% | (22 / 228) | 28.51% | (453 / 1589) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 7 1 1 1 1 1 1 1 5 5 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 141 2 1 1 2 2 2 2 2 2 283 283 2 1 1 2 2 141 137 2 1 1 1 1 5 5 1 1 1 1 1 1 1 1 143 1 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 5 147 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 2 2 137 137 137 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 139 58 1 1 1 1 1 1 1 144 144 7 1 1 1 1 1 1 3 3 1 4 4 1 3 3 1 1 1 1 1 1 | /**
* @license
* Lo-Dash 2.4.2 (Custom Build) <https://lodash.com/>
* Build: `lodash modern -o ./dist/lodash.js`
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <https://lodash.com/license>
*/
;(function() {
/** Used as a safe reference for `undefined` in pre ES5 environments */
var undefined;
/** Used to pool arrays and objects used internally */
var arrayPool = [],
objectPool = [];
/** Used to generate unique IDs */
var idCounter = 0;
/** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
var keyPrefix = +new Date + '';
/** Used as the size when optimizations are enabled for large arrays */
var largeArraySize = 75;
/** Used as the max size of the `arrayPool` and `objectPool` */
var maxPoolSize = 40;
/** Used to detect and test whitespace */
var whitespace = (
// whitespace
' \t\x0B\f\xA0\ufeff' +
// line terminators
'\n\r\u2028\u2029' +
// unicode category "Zs" space separators
'\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
);
/** Used to match empty string literals in compiled template source */
var reEmptyStringLeading = /\b__p \+= '';/g,
reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
/**
* Used to match ES6 template delimiters
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-string-literals
*/
var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
/** Used to match regexp flags from their coerced string values */
var reFlags = /\w*$/;
/** Used to detected named functions */
var reFuncName = /^\s*function[ \n\r\t]+\w/;
/** Used to match "interpolate" template delimiters */
var reInterpolate = /<%=([\s\S]+?)%>/g;
/** Used to match leading whitespace and zeros to be removed */
var reLeadingSpacesAndZeros = RegExp('^[' + whitespace + ']*0+(?=.$)');
/** Used to ensure capturing order of template delimiters */
var reNoMatch = /($^)/;
/** Used to detect functions containing a `this` reference */
var reThis = /\bthis\b/;
/** Used to match unescaped characters in compiled string literals */
var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
/** Used to assign default `context` object properties */
var contextProps = [
'Array', 'Boolean', 'Date', 'Function', 'Math', 'Number', 'Object',
'RegExp', 'String', '_', 'attachEvent', 'clearTimeout', 'isFinite', 'isNaN',
'parseInt', 'setTimeout'
];
/** Used to make template sourceURLs easier to identify */
var templateCounter = 0;
/** `Object#toString` result shortcuts */
var argsClass = '[object Arguments]',
arrayClass = '[object Array]',
boolClass = '[object Boolean]',
dateClass = '[object Date]',
funcClass = '[object Function]',
numberClass = '[object Number]',
objectClass = '[object Object]',
regexpClass = '[object RegExp]',
stringClass = '[object String]';
/** Used to identify object classifications that `_.clone` supports */
var cloneableClasses = {};
cloneableClasses[funcClass] = false;
cloneableClasses[argsClass] = cloneableClasses[arrayClass] =
cloneableClasses[boolClass] = cloneableClasses[dateClass] =
cloneableClasses[numberClass] = cloneableClasses[objectClass] =
cloneableClasses[regexpClass] = cloneableClasses[stringClass] = true;
/** Used as an internal `_.debounce` options object */
var debounceOptions = {
'leading': false,
'maxWait': 0,
'trailing': false
};
/** Used as the property descriptor for `__bindData__` */
var descriptor = {
'configurable': false,
'enumerable': false,
'value': null,
'writable': false
};
/** Used to determine if values are of the language type Object */
var objectTypes = {
'boolean': false,
'function': true,
'object': true,
'number': false,
'string': false,
'undefined': false
};
/** Used to escape characters for inclusion in compiled string literals */
var stringEscapes = {
'\\': '\\',
"'": "'",
'\n': 'n',
'\r': 'r',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
/** Used as a reference to the global object */
var root = (objectTypes[typeof window] && window) || this;
/** Detect free variable `exports` */
var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
/** Detect free variable `module` */
var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
/** Detect the popular CommonJS extension `module.exports` */
var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
/** Detect free variable `global` from Node.js or Browserified code and use it as `root` */
var freeGlobal = objectTypes[typeof global] && global;
Eif (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
root = freeGlobal;
}
/*--------------------------------------------------------------------------*/
/**
* The base implementation of `_.indexOf` without support for binary searches
* or `fromIndex` constraints.
*
* @private
* @param {Array} array The array to search.
* @param {*} value The value to search for.
* @param {number} [fromIndex=0] The index to search from.
* @returns {number} Returns the index of the matched value or `-1`.
*/
function baseIndexOf(array, value, fromIndex) {
var index = (fromIndex || 0) - 1,
length = array ? array.length : 0;
while (++index < length) {
if (array[index] === value) {
return index;
}
}
return -1;
}
/**
* An implementation of `_.contains` for cache objects that mimics the return
* signature of `_.indexOf` by returning `0` if the value is found, else `-1`.
*
* @private
* @param {Object} cache The cache object to inspect.
* @param {*} value The value to search for.
* @returns {number} Returns `0` if `value` is found, else `-1`.
*/
function cacheIndexOf(cache, value) {
var type = typeof value;
cache = cache.cache;
if (type == 'boolean' || value == null) {
return cache[value] ? 0 : -1;
}
if (type != 'number' && type != 'string') {
type = 'object';
}
var key = type == 'number' ? value : keyPrefix + value;
cache = (cache = cache[type]) && cache[key];
return type == 'object'
? (cache && baseIndexOf(cache, value) > -1 ? 0 : -1)
: (cache ? 0 : -1);
}
/**
* Adds a given value to the corresponding cache object.
*
* @private
* @param {*} value The value to add to the cache.
*/
function cachePush(value) {
var cache = this.cache,
type = typeof value;
if (type == 'boolean' || value == null) {
cache[value] = true;
} else {
if (type != 'number' && type != 'string') {
type = 'object';
}
var key = type == 'number' ? value : keyPrefix + value,
typeCache = cache[type] || (cache[type] = {});
if (type == 'object') {
(typeCache[key] || (typeCache[key] = [])).push(value);
} else {
typeCache[key] = true;
}
}
}
/**
* Used by `_.max` and `_.min` as the default callback when a given
* collection is a string value.
*
* @private
* @param {string} value The character to inspect.
* @returns {number} Returns the code unit of given character.
*/
function charAtCallback(value) {
return value.charCodeAt(0);
}
/**
* Used by `sortBy` to compare transformed `collection` elements, stable sorting
* them in ascending order.
*
* @private
* @param {Object} a The object to compare to `b`.
* @param {Object} b The object to compare to `a`.
* @returns {number} Returns the sort order indicator of `1` or `-1`.
*/
function compareAscending(a, b) {
var ac = a.criteria,
bc = b.criteria,
index = -1,
length = ac.length;
while (++index < length) {
var value = ac[index],
other = bc[index];
if (value !== other) {
if (value > other || typeof value == 'undefined') {
return 1;
}
if (value < other || typeof other == 'undefined') {
return -1;
}
}
}
// Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
// that causes it, under certain circumstances, to return the same value for
// `a` and `b`. See https://github.com/jashkenas/underscore/pull/1247
//
// This also ensures a stable sort in V8 and other engines.
// See http://code.google.com/p/v8/issues/detail?id=90
return a.index - b.index;
}
/**
* Creates a cache object to optimize linear searches of large arrays.
*
* @private
* @param {Array} [array=[]] The array to search.
* @returns {null|Object} Returns the cache object or `null` if caching should not be used.
*/
function createCache(array) {
var index = -1,
length = array.length,
first = array[0],
mid = array[(length / 2) | 0],
last = array[length - 1];
if (first && typeof first == 'object' &&
mid && typeof mid == 'object' && last && typeof last == 'object') {
return false;
}
var cache = getObject();
cache['false'] = cache['null'] = cache['true'] = cache['undefined'] = false;
var result = getObject();
result.array = array;
result.cache = cache;
result.push = cachePush;
while (++index < length) {
result.push(array[index]);
}
return result;
}
/**
* Used by `template` to escape characters for inclusion in compiled
* string literals.
*
* @private
* @param {string} match The matched character to escape.
* @returns {string} Returns the escaped character.
*/
function escapeStringChar(match) {
return '\\' + stringEscapes[match];
}
/**
* Gets an array from the array pool or creates a new one if the pool is empty.
*
* @private
* @returns {Array} The array from the pool.
*/
function getArray() {
return arrayPool.pop() || [];
}
/**
* Gets an object from the object pool or creates a new one if the pool is empty.
*
* @private
* @returns {Object} The object from the pool.
*/
function getObject() {
return objectPool.pop() || {
'array': null,
'cache': null,
'criteria': null,
'false': false,
'index': 0,
'null': false,
'number': null,
'object': null,
'push': null,
'string': null,
'true': false,
'undefined': false,
'value': null
};
}
/**
* Releases the given array back to the array pool.
*
* @private
* @param {Array} [array] The array to release.
*/
function releaseArray(array) {
array.length = 0;
if (arrayPool.length < maxPoolSize) {
arrayPool.push(array);
}
}
/**
* Releases the given object back to the object pool.
*
* @private
* @param {Object} [object] The object to release.
*/
function releaseObject(object) {
var cache = object.cache;
if (cache) {
releaseObject(cache);
}
object.array = object.cache = object.criteria = object.object = object.number = object.string = object.value = null;
if (objectPool.length < maxPoolSize) {
objectPool.push(object);
}
}
/**
* Slices the `collection` from the `start` index up to, but not including,
* the `end` index.
*
* Note: This function is used instead of `Array#slice` to support node lists
* in IE < 9 and to ensure dense arrays are returned.
*
* @private
* @param {Array|Object|string} collection The collection to slice.
* @param {number} start The start index.
* @param {number} end The end index.
* @returns {Array} Returns the new array.
*/
function slice(array, start, end) {
start || (start = 0);
if (typeof end == 'undefined') {
end = array ? array.length : 0;
}
var index = -1,
length = end - start || 0,
result = Array(length < 0 ? 0 : length);
while (++index < length) {
result[index] = array[start + index];
}
return result;
}
/*--------------------------------------------------------------------------*/
/**
* Create a new `lodash` function using the given context object.
*
* @static
* @memberOf _
* @category Utilities
* @param {Object} [context=root] The context object.
* @returns {Function} Returns the `lodash` function.
*/
function runInContext(context) {
// Avoid issues with some ES3 environments that attempt to use values, named
// after built-in constructors like `Object`, for the creation of literals.
// ES5 clears this up by stating that literals must use built-in constructors.
// See http://es5.github.io/#x11.1.5.
context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root;
/** Native constructor references */
var Array = context.Array,
Boolean = context.Boolean,
Date = context.Date,
Function = context.Function,
Math = context.Math,
Number = context.Number,
Object = context.Object,
RegExp = context.RegExp,
String = context.String,
TypeError = context.TypeError;
/**
* Used for `Array` method references.
*
* Normally `Array.prototype` would suffice, however, using an array literal
* avoids issues in Narwhal.
*/
var arrayRef = [];
/** Used for native method references */
var objectProto = Object.prototype;
/** Used to restore the original `_` reference in `noConflict` */
var oldDash = context._;
/** Used to resolve the internal [[Class]] of values */
var toString = objectProto.toString;
/** Used to detect if a method is native */
var reNative = RegExp('^' +
String(toString)
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
.replace(/toString| for [^\]]+/g, '.*?') + '$'
);
/** Native method shortcuts */
var ceil = Math.ceil,
clearTimeout = context.clearTimeout,
floor = Math.floor,
fnToString = Function.prototype.toString,
getPrototypeOf = isNative(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
hasOwnProperty = objectProto.hasOwnProperty,
push = arrayRef.push,
setTimeout = context.setTimeout,
splice = arrayRef.splice,
unshift = arrayRef.unshift;
/** Used to set meta data on functions */
var defineProperty = (function() {
// IE 8 only accepts DOM elements
try {
var o = {},
func = isNative(func = Object.defineProperty) && func,
result = func(o, o, o) && func;
} catch(e) { }
return result;
}());
/* Native method shortcuts for methods with the same name as other `lodash` methods */
var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
nativeIsFinite = context.isFinite,
nativeIsNaN = context.isNaN,
nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys,
nativeMax = Math.max,
nativeMin = Math.min,
nativeParseInt = context.parseInt,
nativeRandom = Math.random;
/** Used to lookup a built-in constructor by [[Class]] */
var ctorByClass = {};
ctorByClass[arrayClass] = Array;
ctorByClass[boolClass] = Boolean;
ctorByClass[dateClass] = Date;
ctorByClass[funcClass] = Function;
ctorByClass[objectClass] = Object;
ctorByClass[numberClass] = Number;
ctorByClass[regexpClass] = RegExp;
ctorByClass[stringClass] = String;
/*--------------------------------------------------------------------------*/
/**
* Creates a `lodash` object which wraps the given value to enable intuitive
* method chaining.
*
* In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
* and `unshift`
*
* Chaining is supported in custom builds as long as the `value` method is
* implicitly or explicitly included in the build.
*
* The chainable wrapper functions are:
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
* `compose`, `concat`, `countBy`, `create`, `createCallback`, `curry`,
* `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`,
* `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
* `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `pull`, `push`,
* `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
* `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`,
* `union`, `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`,
* and `zip`
*
* The non-chainable wrapper functions are:
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `findIndex`,
* `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `has`, `identity`,
* `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
* `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, `isNull`, `isNumber`,
* `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, `join`,
* `lastIndexOf`, `mixin`, `noConflict`, `parseInt`, `pop`, `random`, `reduce`,
* `reduceRight`, `result`, `shift`, `size`, `some`, `sortedIndex`, `runInContext`,
* `template`, `unescape`, `uniqueId`, and `value`
*
* The wrapper functions `first` and `last` return wrapped values when `n` is
* provided, otherwise they return unwrapped values.
*
* Explicit chaining can be enabled by using the `_.chain` method.
*
* @name _
* @constructor
* @category Chaining
* @param {*} value The value to wrap in a `lodash` instance.
* @returns {Object} Returns a `lodash` instance.
* @example
*
* var wrapped = _([1, 2, 3]);
*
* // returns an unwrapped value
* wrapped.reduce(function(sum, num) {
* return sum + num;
* });
* // => 6
*
* // returns a wrapped value
* var squares = wrapped.map(function(num) {
* return num * num;
* });
*
* _.isArray(squares);
* // => false
*
* _.isArray(squares.value());
* // => true
*/
function lodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? value
: new lodashWrapper(value);
}
/**
* A fast path for creating `lodash` wrapper objects.
*
* @private
* @param {*} value The value to wrap in a `lodash` instance.
* @param {boolean} chainAll A flag to enable chaining for all methods
* @returns {Object} Returns a `lodash` instance.
*/
function lodashWrapper(value, chainAll) {
this.__chain__ = !!chainAll;
this.__wrapped__ = value;
}
// ensure `new lodashWrapper` is an instance of `lodash`
lodashWrapper.prototype = lodash.prototype;
/**
* An object used to flag environments features.
*
* @static
* @memberOf _
* @type Object
*/
var support = lodash.support = {};
/**
* Detect if functions can be decompiled by `Function#toString`
* (all but PS3 and older Opera mobile browsers & avoided in Windows 8 apps).
*
* @memberOf _.support
* @type boolean
*/
support.funcDecomp = !isNative(context.WinRTError) && reThis.test(runInContext);
/**
* Detect if `Function#name` is supported (all but IE).
*
* @memberOf _.support
* @type boolean
*/
support.funcNames = typeof Function.name == 'string';
/**
* By default, the template delimiters used by Lo-Dash are similar to those in
* embedded Ruby (ERB). Change the following template settings to use alternative
* delimiters.
*
* @static
* @memberOf _
* @type Object
*/
lodash.templateSettings = {
/**
* Used to detect `data` property values to be HTML-escaped.
*
* @memberOf _.templateSettings
* @type RegExp
*/
'escape': /<%-([\s\S]+?)%>/g,
/**
* Used to detect code to be evaluated.
*
* @memberOf _.templateSettings
* @type RegExp
*/
'evaluate': /<%([\s\S]+?)%>/g,
/**
* Used to detect `data` property values to inject.
*
* @memberOf _.templateSettings
* @type RegExp
*/
'interpolate': reInterpolate,
/**
* Used to reference the data object in the template text.
*
* @memberOf _.templateSettings
* @type string
*/
'variable': '',
/**
* Used to import variables into the compiled template.
*
* @memberOf _.templateSettings
* @type Object
*/
'imports': {
/**
* A reference to the `lodash` function.
*
* @memberOf _.templateSettings.imports
* @type Function
*/
'_': lodash
}
};
/*--------------------------------------------------------------------------*/
/**
* The base implementation of `_.bind` that creates the bound function and
* sets its meta data.
*
* @private
* @param {Array} bindData The bind data array.
* @returns {Function} Returns the new bound function.
*/
function baseBind(bindData) {
var func = bindData[0],
partialArgs = bindData[2],
thisArg = bindData[4];
function bound() {
// `Function#bind` spec
// http://es5.github.io/#x15.3.4.5
if (partialArgs) {
// avoid `arguments` object deoptimizations by using `slice` instead
// of `Array.prototype.slice.call` and not assigning `arguments` to a
// variable as a ternary expression
var args = slice(partialArgs);
push.apply(args, arguments);
}
// mimic the constructor's `return` behavior
// http://es5.github.io/#x13.2.2
if (this instanceof bound) {
// ensure `new bound` is an instance of `func`
var thisBinding = baseCreate(func.prototype),
result = func.apply(thisBinding, args || arguments);
return isObject(result) ? result : thisBinding;
}
return func.apply(thisArg, args || arguments);
}
setBindData(bound, bindData);
return bound;
}
/**
* The base implementation of `_.clone` without argument juggling or support
* for `thisArg` binding.
*
* @private
* @param {*} value The value to clone.
* @param {boolean} [isDeep=false] Specify a deep clone.
* @param {Function} [callback] The function to customize cloning values.
* @param {Array} [stackA=[]] Tracks traversed source objects.
* @param {Array} [stackB=[]] Associates clones with source counterparts.
* @returns {*} Returns the cloned value.
*/
function baseClone(value, isDeep, callback, stackA, stackB) {
if (callback) {
var result = callback(value);
if (typeof result != 'undefined') {
return result;
}
}
// inspect [[Class]]
var isObj = isObject(value);
if (isObj) {
var className = toString.call(value);
if (!cloneableClasses[className]) {
return value;
}
var ctor = ctorByClass[className];
switch (className) {
case boolClass:
case dateClass:
return new ctor(+value);
case numberClass:
case stringClass:
return new ctor(value);
case regexpClass:
result = ctor(value.source, reFlags.exec(value));
result.lastIndex = value.lastIndex;
return result;
}
} else {
return value;
}
var isArr = isArray(value);
if (isDeep) {
// check for circular references and return corresponding clone
var initedStack = !stackA;
stackA || (stackA = getArray());
stackB || (stackB = getArray());
var length = stackA.length;
while (length--) {
if (stackA[length] == value) {
return stackB[length];
}
}
result = isArr ? ctor(value.length) : {};
}
else {
result = isArr ? slice(value) : assign({}, value);
}
// add array properties assigned by `RegExp#exec`
if (isArr) {
if (hasOwnProperty.call(value, 'index')) {
result.index = value.index;
}
if (hasOwnProperty.call(value, 'input')) {
result.input = value.input;
}
}
// exit for shallow clone
if (!isDeep) {
return result;
}
// add the source value to the stack of traversed objects
// and associate it with its clone
stackA.push(value);
stackB.push(result);
// recursively populate clone (susceptible to call stack limits)
(isArr ? forEach : forOwn)(value, function(objValue, key) {
result[key] = baseClone(objValue, isDeep, callback, stackA, stackB);
});
if (initedStack) {
releaseArray(stackA);
releaseArray(stackB);
}
return result;
}
/**
* The base implementation of `_.create` without support for assigning
* properties to the created object.
*
* @private
* @param {Object} prototype The object to inherit from.
* @returns {Object} Returns the new object.
*/
function baseCreate(prototype, properties) {
return isObject(prototype) ? nativeCreate(prototype) : {};
}
// fallback for browsers without `Object.create`
Iif (!nativeCreate) {
baseCreate = (function() {
function Object() {}
return function(prototype) {
if (isObject(prototype)) {
Object.prototype = prototype;
var result = new Object;
Object.prototype = null;
}
return result || context.Object();
};
}());
}
/**
* The base implementation of `_.createCallback` without support for creating
* "_.pluck" or "_.where" style callbacks.
*
* @private
* @param {*} [func=identity] The value to convert to a callback.
* @param {*} [thisArg] The `this` binding of the created callback.
* @param {number} [argCount] The number of arguments the callback accepts.
* @returns {Function} Returns a callback function.
*/
function baseCreateCallback(func, thisArg, argCount) {
if (typeof func != 'function') {
return identity;
}
// exit early for no `thisArg` or already bound by `Function#bind`
if (typeof thisArg == 'undefined' || !('prototype' in func)) {
return func;
}
var bindData = func.__bindData__;
if (typeof bindData == 'undefined') {
if (support.funcNames) {
bindData = !func.name;
}
bindData = bindData || !support.funcDecomp;
if (!bindData) {
var source = fnToString.call(func);
if (!support.funcNames) {
bindData = !reFuncName.test(source);
}
if (!bindData) {
// checks if `func` references the `this` keyword and stores the result
bindData = reThis.test(source);
setBindData(func, bindData);
}
}
}
// exit early if there are no `this` references or `func` is bound
if (bindData === false || (bindData !== true && bindData[1] & 1)) {
return func;
}
switch (argCount) {
case 1: return function(value) {
return func.call(thisArg, value);
};
case 2: return function(a, b) {
return func.call(thisArg, a, b);
};
case 3: return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(thisArg, accumulator, value, index, collection);
};
}
return bind(func, thisArg);
}
/**
* The base implementation of `createWrapper` that creates the wrapper and
* sets its meta data.
*
* @private
* @param {Array} bindData The bind data array.
* @returns {Function} Returns the new function.
*/
function baseCreateWrapper(bindData) {
var func = bindData[0],
bitmask = bindData[1],
partialArgs = bindData[2],
partialRightArgs = bindData[3],
thisArg = bindData[4],
arity = bindData[5];
var isBind = bitmask & 1,
isBindKey = bitmask & 2,
isCurry = bitmask & 4,
isCurryBound = bitmask & 8,
key = func;
function bound() {
var thisBinding = isBind ? thisArg : this;
if (partialArgs) {
var args = slice(partialArgs);
push.apply(args, arguments);
}
if (partialRightArgs || isCurry) {
args || (args = slice(arguments));
if (partialRightArgs) {
push.apply(args, partialRightArgs);
}
if (isCurry && args.length < arity) {
bitmask |= 16 & ~32;
return baseCreateWrapper([func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity]);
}
}
args || (args = arguments);
if (isBindKey) {
func = thisBinding[key];
}
if (this instanceof bound) {
thisBinding = baseCreate(func.prototype);
var result = func.apply(thisBinding, args);
return isObject(result) ? result : thisBinding;
}
return func.apply(thisBinding, args);
}
setBindData(bound, bindData);
return bound;
}
/**
* The base implementation of `_.difference` that accepts a single array
* of values to exclude.
*
* @private
* @param {Array} array The array to process.
* @param {Array} [values] The array of values to exclude.
* @returns {Array} Returns a new array of filtered values.
*/
function baseDifference(array, values) {
var index = -1,
indexOf = getIndexOf(),
length = array ? array.length : 0,
isLarge = length >= largeArraySize && indexOf === baseIndexOf,
result = [];
if (isLarge) {
var cache = createCache(values);
if (cache) {
indexOf = cacheIndexOf;
values = cache;
} else {
isLarge = false;
}
}
while (++index < length) {
var value = array[index];
if (indexOf(values, value) < 0) {
result.push(value);
}
}
if (isLarge) {
releaseObject(values);
}
return result;
}
/**
* The base implementation of `_.flatten` without support for callback
* shorthands or `thisArg` binding.
*
* @private
* @param {Array} array The array to flatten.
* @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
* @param {boolean} [isStrict=false] A flag to restrict flattening to arrays and `arguments` objects.
* @param {number} [fromIndex=0] The index to start from.
* @returns {Array} Returns a new flattened array.
*/
function baseFlatten(array, isShallow, isStrict, fromIndex) {
var index = (fromIndex || 0) - 1,
length = array ? array.length : 0,
result = [];
while (++index < length) {
var value = array[index];
if (value && typeof value == 'object' && typeof value.length == 'number'
&& (isArray(value) || isArguments(value))) {
// recursively flatten arrays (susceptible to call stack limits)
if (!isShallow) {
value = baseFlatten(value, isShallow, isStrict);
}
var valIndex = -1,
valLength = value.length,
resIndex = result.length;
result.length += valLength;
while (++valIndex < valLength) {
result[resIndex++] = value[valIndex];
}
} else if (!isStrict) {
result.push(value);
}
}
return result;
}
/**
* The base implementation of `_.isEqual`, without support for `thisArg` binding,
* that allows partial "_.where" style comparisons.
*
* @private
* @param {*} a The value to compare.
* @param {*} b The other value to compare.
* @param {Function} [callback] The function to customize comparing values.
* @param {Function} [isWhere=false] A flag to indicate performing partial comparisons.
* @param {Array} [stackA=[]] Tracks traversed `a` objects.
* @param {Array} [stackB=[]] Tracks traversed `b` objects.
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
*/
function baseIsEqual(a, b, callback, isWhere, stackA, stackB) {
// used to indicate that when comparing objects, `a` has at least the properties of `b`
if (callback) {
var result = callback(a, b);
if (typeof result != 'undefined') {
return !!result;
}
}
// exit early for identical values
if (a === b) {
// treat `+0` vs. `-0` as not equal
return a !== 0 || (1 / a == 1 / b);
}
var type = typeof a,
otherType = typeof b;
// exit early for unlike primitive values
if (a === a &&
!(a && objectTypes[type]) &&
!(b && objectTypes[otherType])) {
return false;
}
// exit early for `null` and `undefined` avoiding ES3's Function#call behavior
// http://es5.github.io/#x15.3.4.4
if (a == null || b == null) {
return a === b;
}
// compare [[Class]] names
var className = toString.call(a),
otherClass = toString.call(b);
if (className == argsClass) {
className = objectClass;
}
if (otherClass == argsClass) {
otherClass = objectClass;
}
if (className != otherClass) {
return false;
}
switch (className) {
case boolClass:
case dateClass:
// coerce dates and booleans to numbers, dates to milliseconds and booleans
// to `1` or `0` treating invalid dates coerced to `NaN` as not equal
return +a == +b;
case numberClass:
// treat `NaN` vs. `NaN` as equal
return (a != +a)
? b != +b
// but treat `+0` vs. `-0` as not equal
: (a == 0 ? (1 / a == 1 / b) : a == +b);
case regexpClass:
case stringClass:
// coerce regexes to strings (http://es5.github.io/#x15.10.6.4)
// treat string primitives and their corresponding object instances as equal
return a == String(b);
}
var isArr = className == arrayClass;
if (!isArr) {
// unwrap any `lodash` wrapped values
var aWrapped = hasOwnProperty.call(a, '__wrapped__'),
bWrapped = hasOwnProperty.call(b, '__wrapped__');
if (aWrapped || bWrapped) {
return baseIsEqual(aWrapped ? a.__wrapped__ : a, bWrapped ? b.__wrapped__ : b, callback, isWhere, stackA, stackB);
}
// exit for functions and DOM nodes
if (className != objectClass) {
return false;
}
// in older versions of Opera, `arguments` objects have `Array` constructors
var ctorA = a.constructor,
ctorB = b.constructor;
// non `Object` object instances with different constructors are not equal
if (ctorA != ctorB &&
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
('constructor' in a && 'constructor' in b)
) {
return false;
}
}
// assume cyclic structures are equal
// the algorithm for detecting cyclic structures is adapted from ES 5.1
// section 15.12.3, abstract operation `JO` (http://es5.github.io/#x15.12.3)
var initedStack = !stackA;
stackA || (stackA = getArray());
stackB || (stackB = getArray());
var length = stackA.length;
while (length--) {
if (stackA[length] == a) {
return stackB[length] == b;
}
}
var size = 0;
result = true;
// add `a` and `b` to the stack of traversed objects
stackA.push(a);
stackB.push(b);
// recursively compare objects and arrays (susceptible to call stack limits)
if (isArr) {
// compare lengths to determine if a deep comparison is necessary
length = a.length;
size = b.length;
result = size == length;
if (result || isWhere) {
// deep compare the contents, ignoring non-numeric properties
while (size--) {
var index = length,
value = b[size];
if (isWhere) {
while (index--) {
if ((result = baseIsEqual(a[index], value, callback, isWhere, stackA, stackB))) {
break;
}
}
} else if (!(result = baseIsEqual(a[size], value, callback, isWhere, stackA, stackB))) {
break;
}
}
}
}
else {
// deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys`
// which, in this case, is more costly
forIn(b, function(value, key, b) {
if (hasOwnProperty.call(b, key)) {
// count the number of properties.
size++;
// deep compare each property value.
return (result = hasOwnProperty.call(a, key) && baseIsEqual(a[key], value, callback, isWhere, stackA, stackB));
}
});
if (result && !isWhere) {
// ensure both objects have the same number of properties
forIn(a, function(value, key, a) {
if (hasOwnProperty.call(a, key)) {
// `size` will be `-1` if `a` has more properties than `b`
return (result = --size > -1);
}
});
}
}
stackA.pop();
stackB.pop();
if (initedStack) {
releaseArray(stackA);
releaseArray(stackB);
}
return result;
}
/**
* The base implementation of `_.merge` without argument juggling or support
* for `thisArg` binding.
*
* @private
* @param {Object} object The destination object.
* @param {Object} source The source object.
* @param {Function} [callback] The function to customize merging properties.
* @param {Array} [stackA=[]] Tracks traversed source objects.
* @param {Array} [stackB=[]] Associates values with source counterparts.
*/
function baseMerge(object, source, callback, stackA, stackB) {
(isArray(source) ? forEach : forOwn)(source, function(source, key) {
var found,
isArr,
result = source,
value = object[key];
if (source && ((isArr = isArray(source)) || isPlainObject(source))) {
// avoid merging previously merged cyclic sources
var stackLength = stackA.length;
while (stackLength--) {
if ((found = stackA[stackLength] == source)) {
value = stackB[stackLength];
break;
}
}
if (!found) {
var isShallow;
if (callback) {
result = callback(value, source);
if ((isShallow = typeof result != 'undefined')) {
value = result;
}
}
if (!isShallow) {
value = isArr
? (isArray(value) ? value : [])
: (isPlainObject(value) ? value : {});
}
// add `source` and associated `value` to the stack of traversed objects
stackA.push(source);
stackB.push(value);
// recursively merge objects and arrays (susceptible to call stack limits)
if (!isShallow) {
baseMerge(value, source, callback, stackA, stackB);
}
}
}
else {
if (callback) {
result = callback(value, source);
if (typeof result == 'undefined') {
result = source;
}
}
if (typeof result != 'undefined') {
value = result;
}
}
object[key] = value;
});
}
/**
* The base implementation of `_.random` without argument juggling or support
* for returning floating-point numbers.
*
* @private
* @param {number} min The minimum possible value.
* @param {number} max The maximum possible value.
* @returns {number} Returns a random number.
*/
function baseRandom(min, max) {
return min + floor(nativeRandom() * (max - min + 1));
}
/**
* The base implementation of `_.uniq` without support for callback shorthands
* or `thisArg` binding.
*
* @private
* @param {Array} array The array to process.
* @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
* @param {Function} [callback] The function called per iteration.
* @returns {Array} Returns a duplicate-value-free array.
*/
function baseUniq(array, isSorted, callback) {
var index = -1,
indexOf = getIndexOf(),
length = array ? array.length : 0,
result = [];
var isLarge = !isSorted && length >= largeArraySize && indexOf === baseIndexOf,
seen = (callback || isLarge) ? getArray() : result;
if (isLarge) {
var cache = createCache(seen);
indexOf = cacheIndexOf;
seen = cache;
}
while (++index < length) {
var value = array[index],
computed = callback ? callback(value, index, array) : value;
if (isSorted
? !index || seen[seen.length - 1] !== computed
: indexOf(seen, computed) < 0
) {
if (callback || isLarge) {
seen.push(computed);
}
result.push(value);
}
}
if (isLarge) {
releaseArray(seen.array);
releaseObject(seen);
} else if (callback) {
releaseArray(seen);
}
return result;
}
/**
* Creates a function that aggregates a collection, creating an object composed
* of keys generated from the results of running each element of the collection
* through a callback. The given `setter` function sets the keys and values
* of the composed object.
*
* @private
* @param {Function} setter The setter function.
* @returns {Function} Returns the new aggregator function.
*/
function createAggregator(setter) {
return function(collection, callback, thisArg) {
var result = {};
callback = lodash.createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
while (++index < length) {
var value = collection[index];
setter(result, value, callback(value, index, collection), collection);
}
} else {
forOwn(collection, function(value, key, collection) {
setter(result, value, callback(value, key, collection), collection);
});
}
return result;
};
}
/**
* Creates a function that, when called, either curries or invokes `func`
* with an optional `this` binding and partially applied arguments.
*
* @private
* @param {Function|string} func The function or method name to reference.
* @param {number} bitmask The bitmask of method flags to compose.
* The bitmask may be composed of the following flags:
* 1 - `_.bind`
* 2 - `_.bindKey`
* 4 - `_.curry`
* 8 - `_.curry` (bound)
* 16 - `_.partial`
* 32 - `_.partialRight`
* @param {Array} [partialArgs] An array of arguments to prepend to those
* provided to the new function.
* @param {Array} [partialRightArgs] An array of arguments to append to those
* provided to the new function.
* @param {*} [thisArg] The `this` binding of `func`.
* @param {number} [arity] The arity of `func`.
* @returns {Function} Returns the new function.
*/
function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
var isBind = bitmask & 1,
isBindKey = bitmask & 2,
isCurry = bitmask & 4,
isCurryBound = bitmask & 8,
isPartial = bitmask & 16,
isPartialRight = bitmask & 32;
if (!isBindKey && !isFunction(func)) {
throw new TypeError;
}
if (isPartial && !partialArgs.length) {
bitmask &= ~16;
isPartial = partialArgs = false;
}
if (isPartialRight && !partialRightArgs.length) {
bitmask &= ~32;
isPartialRight = partialRightArgs = false;
}
var bindData = func && func.__bindData__;
if (bindData && bindData !== true) {
// clone `bindData`
bindData = slice(bindData);
if (bindData[2]) {
bindData[2] = slice(bindData[2]);
}
if (bindData[3]) {
bindData[3] = slice(bindData[3]);
}
// set `thisBinding` is not previously bound
if (isBind && !(bindData[1] & 1)) {
bindData[4] = thisArg;
}
// set if previously bound but not currently (subsequent curried functions)
if (!isBind && bindData[1] & 1) {
bitmask |= 8;
}
// set curried arity if not yet set
if (isCurry && !(bindData[1] & 4)) {
bindData[5] = arity;
}
// append partial left arguments
if (isPartial) {
push.apply(bindData[2] || (bindData[2] = []), partialArgs);
}
// append partial right arguments
if (isPartialRight) {
unshift.apply(bindData[3] || (bindData[3] = []), partialRightArgs);
}
// merge flags
bindData[1] |= bitmask;
return createWrapper.apply(null, bindData);
}
// fast path for `_.bind`
var creater = (bitmask == 1 || bitmask === 17) ? baseBind : baseCreateWrapper;
return creater([func, bitmask, partialArgs, partialRightArgs, thisArg, arity]);
}
/**
* Used by `escape` to convert characters to HTML entities.
*
* @private
* @param {string} match The matched character to escape.
* @returns {string} Returns the escaped character.
*/
function escapeHtmlChar(match) {
return htmlEscapes[match];
}
/**
* Gets the appropriate "indexOf" function. If the `_.indexOf` method is
* customized, this method returns the custom method, otherwise it returns
* the `baseIndexOf` function.
*
* @private
* @returns {Function} Returns the "indexOf" function.
*/
function getIndexOf() {
var result = (result = lodash.indexOf) === indexOf ? baseIndexOf : result;
return result;
}
/**
* Checks if `value` is a native function.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a native function, else `false`.
*/
function isNative(value) {
return typeof value == 'function' && reNative.test(value);
}
/**
* Sets `this` binding data on a given function.
*
* @private
* @param {Function} func The function to set data on.
* @param {Array} value The data array to set.
*/
var setBindData = !defineProperty ? noop : function(func, value) {
descriptor.value = value;
defineProperty(func, '__bindData__', descriptor);
descriptor.value = null;
};
/**
* A fallback implementation of `isPlainObject` which checks if a given value
* is an object created by the `Object` constructor, assuming objects created
* by the `Object` constructor have no inherited enumerable properties and that
* there are no `Object.prototype` extensions.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
*/
function shimIsPlainObject(value) {
var ctor,
result;
// avoid non Object objects, `arguments` objects, and DOM elements
if (!(value && toString.call(value) == objectClass) ||
(ctor = value.constructor, isFunction(ctor) && !(ctor instanceof ctor))) {
return false;
}
// In most environments an object's own properties are iterated before
// its inherited properties. If the last iterated property is an object's
// own property then there are no inherited enumerable properties.
forIn(value, function(value, key) {
result = key;
});
return typeof result == 'undefined' || hasOwnProperty.call(value, result);
}
/**
* Used by `unescape` to convert HTML entities to characters.
*
* @private
* @param {string} match The matched character to unescape.
* @returns {string} Returns the unescaped character.
*/
function unescapeHtmlChar(match) {
return htmlUnescapes[match];
}
/*--------------------------------------------------------------------------*/
/**
* Checks if `value` is an `arguments` object.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
* @example
*
* (function() { return _.isArguments(arguments); })(1, 2, 3);
* // => true
*
* _.isArguments([1, 2, 3]);
* // => false
*/
function isArguments(value) {
return value && typeof value == 'object' && typeof value.length == 'number' &&
toString.call(value) == argsClass || false;
}
/**
* Checks if `value` is an array.
*
* @static
* @memberOf _
* @type Function
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is an array, else `false`.
* @example
*
* (function() { return _.isArray(arguments); })();
* // => false
*
* _.isArray([1, 2, 3]);
* // => true
*/
var isArray = nativeIsArray || function(value) {
return value && typeof value == 'object' && typeof value.length == 'number' &&
toString.call(value) == arrayClass || false;
};
/**
* A fallback implementation of `Object.keys` which produces an array of the
* given object's own enumerable property names.
*
* @private
* @type Function
* @param {Object} object The object to inspect.
* @returns {Array} Returns an array of property names.
*/
var shimKeys = function(object) {
var index, iterable = object, result = [];
if (!iterable) return result;
if (!(objectTypes[typeof object])) return result;
for (index in iterable) {
if (hasOwnProperty.call(iterable, index)) {
result.push(index);
}
}
return result
};
/**
* Creates an array composed of the own enumerable property names of an object.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @returns {Array} Returns an array of property names.
* @example
*
* _.keys({ 'one': 1, 'two': 2, 'three': 3 });
* // => ['one', 'two', 'three'] (property order is not guaranteed across environments)
*/
var keys = !nativeKeys ? shimKeys : function(object) {
Iif (!isObject(object)) {
return [];
}
return nativeKeys(object);
};
/**
* Used to convert characters to HTML entities:
*
* Though the `>` character is escaped for symmetry, characters like `>` and `/`
* don't require escaping in HTML and have no special meaning unless they're part
* of a tag or an unquoted attribute value.
* http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
*/
var htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
/** Used to convert HTML entities to characters */
var htmlUnescapes = invert(htmlEscapes);
/** Used to match HTML entities and HTML characters */
var reEscapedHtml = RegExp('(' + keys(htmlUnescapes).join('|') + ')', 'g'),
reUnescapedHtml = RegExp('[' + keys(htmlEscapes).join('') + ']', 'g');
/*--------------------------------------------------------------------------*/
/**
* Assigns own enumerable properties of source object(s) to the destination
* object. Subsequent sources will overwrite property assignments of previous
* sources. If a callback is provided it will be executed to produce the
* assigned values. The callback is bound to `thisArg` and invoked with two
* arguments; (objectValue, sourceValue).
*
* @static
* @memberOf _
* @type Function
* @alias extend
* @category Objects
* @param {Object} object The destination object.
* @param {...Object} [source] The source objects.
* @param {Function} [callback] The function to customize assigning values.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns the destination object.
* @example
*
* _.assign({ 'name': 'fred' }, { 'employer': 'slate' });
* // => { 'name': 'fred', 'employer': 'slate' }
*
* var defaults = _.partialRight(_.assign, function(a, b) {
* return typeof a == 'undefined' ? b : a;
* });
*
* var object = { 'name': 'barney' };
* defaults(object, { 'name': 'fred', 'employer': 'slate' });
* // => { 'name': 'barney', 'employer': 'slate' }
*/
var assign = function(object, source, guard) {
var index, iterable = object, result = iterable;
if (!iterable) return result;
var args = arguments,
argsIndex = 0,
argsLength = typeof guard == 'number' ? 2 : args.length;
if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {
var callback = baseCreateCallback(args[--argsLength - 1], args[argsLength--], 2);
} else if (argsLength > 2 && typeof args[argsLength - 1] == 'function') {
callback = args[--argsLength];
}
while (++argsIndex < argsLength) {
iterable = args[argsIndex];
if (iterable && objectTypes[typeof iterable]) {
var ownIndex = -1,
ownProps = objectTypes[typeof iterable] && keys(iterable),
length = ownProps ? ownProps.length : 0;
while (++ownIndex < length) {
index = ownProps[ownIndex];
result[index] = callback ? callback(result[index], iterable[index]) : iterable[index];
}
}
}
return result
};
/**
* Creates a clone of `value`. If `isDeep` is `true` nested objects will also
* be cloned, otherwise they will be assigned by reference. If a callback
* is provided it will be executed to produce the cloned values. If the
* callback returns `undefined` cloning will be handled by the method instead.
* The callback is bound to `thisArg` and invoked with one argument; (value).
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to clone.
* @param {boolean} [isDeep=false] Specify a deep clone.
* @param {Function} [callback] The function to customize cloning values.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the cloned value.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* var shallow = _.clone(characters);
* shallow[0] === characters[0];
* // => true
*
* var deep = _.clone(characters, true);
* deep[0] === characters[0];
* // => false
*
* _.mixin({
* 'clone': _.partialRight(_.clone, function(value) {
* return _.isElement(value) ? value.cloneNode(false) : undefined;
* })
* });
*
* var clone = _.clone(document.body);
* clone.childNodes.length;
* // => 0
*/
function clone(value, isDeep, callback, thisArg) {
// allows working with "Collections" methods without using their `index`
// and `collection` arguments for `isDeep` and `callback`
if (typeof isDeep != 'boolean' && isDeep != null) {
thisArg = callback;
callback = isDeep;
isDeep = false;
}
return baseClone(value, isDeep, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
}
/**
* Creates a deep clone of `value`. If a callback is provided it will be
* executed to produce the cloned values. If the callback returns `undefined`
* cloning will be handled by the method instead. The callback is bound to
* `thisArg` and invoked with one argument; (value).
*
* Note: This method is loosely based on the structured clone algorithm. Functions
* and DOM nodes are **not** cloned. The enumerable properties of `arguments` objects and
* objects created by constructors other than `Object` are cloned to plain `Object` objects.
* See http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to deep clone.
* @param {Function} [callback] The function to customize cloning values.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the deep cloned value.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* var deep = _.cloneDeep(characters);
* deep[0] === characters[0];
* // => false
*
* var view = {
* 'label': 'docs',
* 'node': element
* };
*
* var clone = _.cloneDeep(view, function(value) {
* return _.isElement(value) ? value.cloneNode(true) : undefined;
* });
*
* clone.node == view.node;
* // => false
*/
function cloneDeep(value, callback, thisArg) {
return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
}
/**
* Creates an object that inherits from the given `prototype` object. If a
* `properties` object is provided its own enumerable properties are assigned
* to the created object.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} prototype The object to inherit from.
* @param {Object} [properties] The properties to assign to the object.
* @returns {Object} Returns the new object.
* @example
*
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* function Circle() {
* Shape.call(this);
* }
*
* Circle.prototype = _.create(Shape.prototype, { 'constructor': Circle });
*
* var circle = new Circle;
* circle instanceof Circle;
* // => true
*
* circle instanceof Shape;
* // => true
*/
function create(prototype, properties) {
var result = baseCreate(prototype);
return properties ? assign(result, properties) : result;
}
/**
* Assigns own enumerable properties of source object(s) to the destination
* object for all destination properties that resolve to `undefined`. Once a
* property is set, additional defaults of the same property will be ignored.
*
* @static
* @memberOf _
* @type Function
* @category Objects
* @param {Object} object The destination object.
* @param {...Object} [source] The source objects.
* @param- {Object} [guard] Allows working with `_.reduce` without using its
* `key` and `object` arguments as sources.
* @returns {Object} Returns the destination object.
* @example
*
* var object = { 'name': 'barney' };
* _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
* // => { 'name': 'barney', 'employer': 'slate' }
*/
var defaults = function(object, source, guard) {
var index, iterable = object, result = iterable;
if (!iterable) return result;
var args = arguments,
argsIndex = 0,
argsLength = typeof guard == 'number' ? 2 : args.length;
while (++argsIndex < argsLength) {
iterable = args[argsIndex];
if (iterable && objectTypes[typeof iterable]) {
var ownIndex = -1,
ownProps = objectTypes[typeof iterable] && keys(iterable),
length = ownProps ? ownProps.length : 0;
while (++ownIndex < length) {
index = ownProps[ownIndex];
if (typeof result[index] == 'undefined') result[index] = iterable[index];
}
}
}
return result
};
/**
* This method is like `_.findIndex` except that it returns the key of the
* first element that passes the callback check, instead of the element itself.
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to search.
* @param {Function|Object|string} [callback=identity] The function called per
* iteration. If a property name or object is provided it will be used to
* create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {string|undefined} Returns the key of the found element, else `undefined`.
* @example
*
* var characters = {
* 'barney': { 'age': 36, 'blocked': false },
* 'fred': { 'age': 40, 'blocked': true },
* 'pebbles': { 'age': 1, 'blocked': false }
* };
*
* _.findKey(characters, function(chr) {
* return chr.age < 40;
* });
* // => 'barney' (property order is not guaranteed across environments)
*
* // using "_.where" callback shorthand
* _.findKey(characters, { 'age': 1 });
* // => 'pebbles'
*
* // using "_.pluck" callback shorthand
* _.findKey(characters, 'blocked');
* // => 'fred'
*/
function findKey(object, callback, thisArg) {
var result;
callback = lodash.createCallback(callback, thisArg, 3);
forOwn(object, function(value, key, object) {
if (callback(value, key, object)) {
result = key;
return false;
}
});
return result;
}
/**
* This method is like `_.findKey` except that it iterates over elements
* of a `collection` in the opposite order.
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to search.
* @param {Function|Object|string} [callback=identity] The function called per
* iteration. If a property name or object is provided it will be used to
* create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {string|undefined} Returns the key of the found element, else `undefined`.
* @example
*
* var characters = {
* 'barney': { 'age': 36, 'blocked': true },
* 'fred': { 'age': 40, 'blocked': false },
* 'pebbles': { 'age': 1, 'blocked': true }
* };
*
* _.findLastKey(characters, function(chr) {
* return chr.age < 40;
* });
* // => returns `pebbles`, assuming `_.findKey` returns `barney`
*
* // using "_.where" callback shorthand
* _.findLastKey(characters, { 'age': 40 });
* // => 'fred'
*
* // using "_.pluck" callback shorthand
* _.findLastKey(characters, 'blocked');
* // => 'pebbles'
*/
function findLastKey(object, callback, thisArg) {
var result;
callback = lodash.createCallback(callback, thisArg, 3);
forOwnRight(object, function(value, key, object) {
if (callback(value, key, object)) {
result = key;
return false;
}
});
return result;
}
/**
* Iterates over own and inherited enumerable properties of an object,
* executing the callback for each property. The callback is bound to `thisArg`
* and invoked with three arguments; (value, key, object). Callbacks may exit
* iteration early by explicitly returning `false`.
*
* @static
* @memberOf _
* @type Function
* @category Objects
* @param {Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`.
* @example
*
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* Shape.prototype.move = function(x, y) {
* this.x += x;
* this.y += y;
* };
*
* _.forIn(new Shape, function(value, key) {
* console.log(key);
* });
* // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments)
*/
var forIn = function(collection, callback, thisArg) {
var index, iterable = collection, result = iterable;
Iif (!iterable) return result;
Iif (!objectTypes[typeof iterable]) return result;
callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
for (index in iterable) {
Iif (callback(iterable[index], index, collection) === false) return result;
}
return result
};
/**
* This method is like `_.forIn` except that it iterates over elements
* of a `collection` in the opposite order.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`.
* @example
*
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* Shape.prototype.move = function(x, y) {
* this.x += x;
* this.y += y;
* };
*
* _.forInRight(new Shape, function(value, key) {
* console.log(key);
* });
* // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move'
*/
function forInRight(object, callback, thisArg) {
var pairs = [];
forIn(object, function(value, key) {
pairs.push(key, value);
});
var length = pairs.length;
callback = baseCreateCallback(callback, thisArg, 3);
while (length--) {
if (callback(pairs[length--], pairs[length], object) === false) {
break;
}
}
return object;
}
/**
* Iterates over own enumerable properties of an object, executing the callback
* for each property. The callback is bound to `thisArg` and invoked with three
* arguments; (value, key, object). Callbacks may exit iteration early by
* explicitly returning `false`.
*
* @static
* @memberOf _
* @type Function
* @category Objects
* @param {Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`.
* @example
*
* _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
* console.log(key);
* });
* // => logs '0', '1', and 'length' (property order is not guaranteed across environments)
*/
var forOwn = function(collection, callback, thisArg) {
var index, iterable = collection, result = iterable;
Iif (!iterable) return result;
Iif (!objectTypes[typeof iterable]) return result;
callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
var ownIndex = -1,
ownProps = objectTypes[typeof iterable] && keys(iterable),
length = ownProps ? ownProps.length : 0;
while (++ownIndex < length) {
index = ownProps[ownIndex];
Iif (callback(iterable[index], index, collection) === false) return result;
}
return result
};
/**
* This method is like `_.forOwn` except that it iterates over elements
* of a `collection` in the opposite order.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`.
* @example
*
* _.forOwnRight({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
* console.log(key);
* });
* // => logs 'length', '1', and '0' assuming `_.forOwn` logs '0', '1', and 'length'
*/
function forOwnRight(object, callback, thisArg) {
var props = keys(object),
length = props.length;
callback = baseCreateCallback(callback, thisArg, 3);
while (length--) {
var key = props[length];
if (callback(object[key], key, object) === false) {
break;
}
}
return object;
}
/**
* Creates a sorted array of property names of all enumerable properties,
* own and inherited, of `object` that have function values.
*
* @static
* @memberOf _
* @alias methods
* @category Objects
* @param {Object} object The object to inspect.
* @returns {Array} Returns an array of property names that have function values.
* @example
*
* _.functions(_);
* // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
*/
function functions(object) {
var result = [];
forIn(object, function(value, key) {
if (isFunction(value)) {
result.push(key);
}
});
return result.sort();
}
/**
* Checks if the specified property name exists as a direct property of `object`,
* instead of an inherited property.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @param {string} key The name of the property to check.
* @returns {boolean} Returns `true` if key is a direct property, else `false`.
* @example
*
* _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
* // => true
*/
function has(object, key) {
return object ? hasOwnProperty.call(object, key) : false;
}
/**
* Creates an object composed of the inverted keys and values of the given object.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to invert.
* @returns {Object} Returns the created inverted object.
* @example
*
* _.invert({ 'first': 'fred', 'second': 'barney' });
* // => { 'fred': 'first', 'barney': 'second' }
*/
function invert(object) {
var index = -1,
props = keys(object),
length = props.length,
result = {};
while (++index < length) {
var key = props[index];
result[object[key]] = key;
}
return result;
}
/**
* Checks if `value` is a boolean value.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a boolean value, else `false`.
* @example
*
* _.isBoolean(null);
* // => false
*/
function isBoolean(value) {
return value === true || value === false ||
value && typeof value == 'object' && toString.call(value) == boolClass || false;
}
/**
* Checks if `value` is a date.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a date, else `false`.
* @example
*
* _.isDate(new Date);
* // => true
*/
function isDate(value) {
return value && typeof value == 'object' && toString.call(value) == dateClass || false;
}
/**
* Checks if `value` is a DOM element.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a DOM element, else `false`.
* @example
*
* _.isElement(document.body);
* // => true
*/
function isElement(value) {
return value && value.nodeType === 1 || false;
}
/**
* Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
* length of `0` and objects with no own enumerable properties are considered
* "empty".
*
* @static
* @memberOf _
* @category Objects
* @param {Array|Object|string} value The value to inspect.
* @returns {boolean} Returns `true` if the `value` is empty, else `false`.
* @example
*
* _.isEmpty([1, 2, 3]);
* // => false
*
* _.isEmpty({});
* // => true
*
* _.isEmpty('');
* // => true
*/
function isEmpty(value) {
var result = true;
if (!value) {
return result;
}
var className = toString.call(value),
length = value.length;
if ((className == arrayClass || className == stringClass || className == argsClass ) ||
(className == objectClass && typeof length == 'number' && isFunction(value.splice))) {
return !length;
}
forOwn(value, function() {
return (result = false);
});
return result;
}
/**
* Performs a deep comparison between two values to determine if they are
* equivalent to each other. If a callback is provided it will be executed
* to compare values. If the callback returns `undefined` comparisons will
* be handled by the method instead. The callback is bound to `thisArg` and
* invoked with two arguments; (a, b).
*
* @static
* @memberOf _
* @category Objects
* @param {*} a The value to compare.
* @param {*} b The other value to compare.
* @param {Function} [callback] The function to customize comparing values.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
* @example
*
* var object = { 'name': 'fred' };
* var copy = { 'name': 'fred' };
*
* object == copy;
* // => false
*
* _.isEqual(object, copy);
* // => true
*
* var words = ['hello', 'goodbye'];
* var otherWords = ['hi', 'goodbye'];
*
* _.isEqual(words, otherWords, function(a, b) {
* var reGreet = /^(?:hello|hi)$/i,
* aGreet = _.isString(a) && reGreet.test(a),
* bGreet = _.isString(b) && reGreet.test(b);
*
* return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
* });
* // => true
*/
function isEqual(a, b, callback, thisArg) {
return baseIsEqual(a, b, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 2));
}
/**
* Checks if `value` is, or can be coerced to, a finite number.
*
* Note: This is not the same as native `isFinite` which will return true for
* booleans and empty strings. See http://es5.github.io/#x15.1.2.5.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is finite, else `false`.
* @example
*
* _.isFinite(-101);
* // => true
*
* _.isFinite('10');
* // => true
*
* _.isFinite(true);
* // => false
*
* _.isFinite('');
* // => false
*
* _.isFinite(Infinity);
* // => false
*/
function isFinite(value) {
return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
}
/**
* Checks if `value` is a function.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a function, else `false`.
* @example
*
* _.isFunction(_);
* // => true
*/
function isFunction(value) {
return typeof value == 'function';
}
/**
* Checks if `value` is the language type of Object.
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is an object, else `false`.
* @example
*
* _.isObject({});
* // => true
*
* _.isObject([1, 2, 3]);
* // => true
*
* _.isObject(1);
* // => false
*/
function isObject(value) {
// check if the value is the ECMAScript language type of Object
// http://es5.github.io/#x8
// and avoid a V8 bug
// http://code.google.com/p/v8/issues/detail?id=2291
return !!(value && objectTypes[typeof value]);
}
/**
* Checks if `value` is `NaN`.
*
* Note: This is not the same as native `isNaN` which will return `true` for
* `undefined` and other non-numeric values. See http://es5.github.io/#x15.1.2.4.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is `NaN`, else `false`.
* @example
*
* _.isNaN(NaN);
* // => true
*
* _.isNaN(new Number(NaN));
* // => true
*
* isNaN(undefined);
* // => true
*
* _.isNaN(undefined);
* // => false
*/
function isNaN(value) {
// `NaN` as a primitive is the only value that is not equal to itself
// (perform the [[Class]] check first to avoid errors with some host objects in IE)
return isNumber(value) && value != +value;
}
/**
* Checks if `value` is `null`.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is `null`, else `false`.
* @example
*
* _.isNull(null);
* // => true
*
* _.isNull(undefined);
* // => false
*/
function isNull(value) {
return value === null;
}
/**
* Checks if `value` is a number.
*
* Note: `NaN` is considered a number. See http://es5.github.io/#x8.5.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a number, else `false`.
* @example
*
* _.isNumber(8.4 * 5);
* // => true
*/
function isNumber(value) {
return typeof value == 'number' ||
value && typeof value == 'object' && toString.call(value) == numberClass || false;
}
/**
* Checks if `value` is an object created by the `Object` constructor.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
* @example
*
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* _.isPlainObject(new Shape);
* // => false
*
* _.isPlainObject([1, 2, 3]);
* // => false
*
* _.isPlainObject({ 'x': 0, 'y': 0 });
* // => true
*/
var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) {
if (!(value && toString.call(value) == objectClass)) {
return false;
}
var valueOf = value.valueOf,
objProto = isNative(valueOf) && (objProto = getPrototypeOf(valueOf)) && getPrototypeOf(objProto);
return objProto
? (value == objProto || getPrototypeOf(value) == objProto)
: shimIsPlainObject(value);
};
/**
* Checks if `value` is a regular expression.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a regular expression, else `false`.
* @example
*
* _.isRegExp(/fred/);
* // => true
*/
function isRegExp(value) {
return value && typeof value == 'object' && toString.call(value) == regexpClass || false;
}
/**
* Checks if `value` is a string.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is a string, else `false`.
* @example
*
* _.isString('fred');
* // => true
*/
function isString(value) {
return typeof value == 'string' ||
value && typeof value == 'object' && toString.call(value) == stringClass || false;
}
/**
* Checks if `value` is `undefined`.
*
* @static
* @memberOf _
* @category Objects
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if the `value` is `undefined`, else `false`.
* @example
*
* _.isUndefined(void 0);
* // => true
*/
function isUndefined(value) {
return typeof value == 'undefined';
}
/**
* Creates an object with the same keys as `object` and values generated by
* running each own enumerable property of `object` through the callback.
* The callback is bound to `thisArg` and invoked with three arguments;
* (value, key, object).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new object with values of the results of each `callback` execution.
* @example
*
* _.mapValues({ 'a': 1, 'b': 2, 'c': 3} , function(num) { return num * 3; });
* // => { 'a': 3, 'b': 6, 'c': 9 }
*
* var characters = {
* 'fred': { 'name': 'fred', 'age': 40 },
* 'pebbles': { 'name': 'pebbles', 'age': 1 }
* };
*
* // using "_.pluck" callback shorthand
* _.mapValues(characters, 'age');
* // => { 'fred': 40, 'pebbles': 1 }
*/
function mapValues(object, callback, thisArg) {
var result = {};
callback = lodash.createCallback(callback, thisArg, 3);
forOwn(object, function(value, key, object) {
result[key] = callback(value, key, object);
});
return result;
}
/**
* Recursively merges own enumerable properties of the source object(s), that
* don't resolve to `undefined` into the destination object. Subsequent sources
* will overwrite property assignments of previous sources. If a callback is
* provided it will be executed to produce the merged values of the destination
* and source properties. If the callback returns `undefined` merging will
* be handled by the method instead. The callback is bound to `thisArg` and
* invoked with two arguments; (objectValue, sourceValue).
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The destination object.
* @param {...Object} [source] The source objects.
* @param {Function} [callback] The function to customize merging properties.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns the destination object.
* @example
*
* var names = {
* 'characters': [
* { 'name': 'barney' },
* { 'name': 'fred' }
* ]
* };
*
* var ages = {
* 'characters': [
* { 'age': 36 },
* { 'age': 40 }
* ]
* };
*
* _.merge(names, ages);
* // => { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] }
*
* var food = {
* 'fruits': ['apple'],
* 'vegetables': ['beet']
* };
*
* var otherFood = {
* 'fruits': ['banana'],
* 'vegetables': ['carrot']
* };
*
* _.merge(food, otherFood, function(a, b) {
* return _.isArray(a) ? a.concat(b) : undefined;
* });
* // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }
*/
function merge(object) {
var args = arguments,
length = 2;
if (!isObject(object)) {
return object;
}
// allows working with `_.reduce` and `_.reduceRight` without using
// their `index` and `collection` arguments
if (typeof args[2] != 'number') {
length = args.length;
}
if (length > 3 && typeof args[length - 2] == 'function') {
var callback = baseCreateCallback(args[--length - 1], args[length--], 2);
} else if (length > 2 && typeof args[length - 1] == 'function') {
callback = args[--length];
}
var sources = slice(arguments, 1, length),
index = -1,
stackA = getArray(),
stackB = getArray();
while (++index < length) {
baseMerge(object, sources[index], callback, stackA, stackB);
}
releaseArray(stackA);
releaseArray(stackB);
return object;
}
/**
* Creates a shallow clone of `object` excluding the specified properties.
* Property names may be specified as individual arguments or as arrays of
* property names. If a callback is provided it will be executed for each
* property of `object` omitting the properties the callback returns truey
* for. The callback is bound to `thisArg` and invoked with three arguments;
* (value, key, object).
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The source object.
* @param {Function|...string|string[]} [callback] The properties to omit or the
* function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns an object without the omitted properties.
* @example
*
* _.omit({ 'name': 'fred', 'age': 40 }, 'age');
* // => { 'name': 'fred' }
*
* _.omit({ 'name': 'fred', 'age': 40 }, function(value) {
* return typeof value == 'number';
* });
* // => { 'name': 'fred' }
*/
function omit(object, callback, thisArg) {
var result = {};
if (typeof callback != 'function') {
var props = [];
forIn(object, function(value, key) {
props.push(key);
});
props = baseDifference(props, baseFlatten(arguments, true, false, 1));
var index = -1,
length = props.length;
while (++index < length) {
var key = props[index];
result[key] = object[key];
}
} else {
callback = lodash.createCallback(callback, thisArg, 3);
forIn(object, function(value, key, object) {
if (!callback(value, key, object)) {
result[key] = value;
}
});
}
return result;
}
/**
* Creates a two dimensional array of an object's key-value pairs,
* i.e. `[[key1, value1], [key2, value2]]`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @returns {Array} Returns new array of key-value pairs.
* @example
*
* _.pairs({ 'barney': 36, 'fred': 40 });
* // => [['barney', 36], ['fred', 40]] (property order is not guaranteed across environments)
*/
function pairs(object) {
var index = -1,
props = keys(object),
length = props.length,
result = Array(length);
while (++index < length) {
var key = props[index];
result[index] = [key, object[key]];
}
return result;
}
/**
* Creates a shallow clone of `object` composed of the specified properties.
* Property names may be specified as individual arguments or as arrays of
* property names. If a callback is provided it will be executed for each
* property of `object` picking the properties the callback returns truey
* for. The callback is bound to `thisArg` and invoked with three arguments;
* (value, key, object).
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The source object.
* @param {Function|...string|string[]} [callback] The function called per
* iteration or property names to pick, specified as individual property
* names or arrays of property names.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns an object composed of the picked properties.
* @example
*
* _.pick({ 'name': 'fred', '_userid': 'fred1' }, 'name');
* // => { 'name': 'fred' }
*
* _.pick({ 'name': 'fred', '_userid': 'fred1' }, function(value, key) {
* return key.charAt(0) != '_';
* });
* // => { 'name': 'fred' }
*/
function pick(object, callback, thisArg) {
var result = {};
if (typeof callback != 'function') {
var index = -1,
props = baseFlatten(arguments, true, false, 1),
length = isObject(object) ? props.length : 0;
while (++index < length) {
var key = props[index];
if (key in object) {
result[key] = object[key];
}
}
} else {
callback = lodash.createCallback(callback, thisArg, 3);
forIn(object, function(value, key, object) {
if (callback(value, key, object)) {
result[key] = value;
}
});
}
return result;
}
/**
* An alternative to `_.reduce` this method transforms `object` to a new
* `accumulator` object which is the result of running each of its own
* enumerable properties through a callback, with each callback execution
* potentially mutating the `accumulator` object. The callback is bound to
* `thisArg` and invoked with four arguments; (accumulator, value, key, object).
* Callbacks may exit iteration early by explicitly returning `false`.
*
* @static
* @memberOf _
* @category Objects
* @param {Array|Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [accumulator] The custom accumulator value.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the accumulated value.
* @example
*
* var squares = _.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(result, num) {
* num *= num;
* if (num % 2) {
* return result.push(num) < 3;
* }
* });
* // => [1, 9, 25]
*
* var mapped = _.transform({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
* result[key] = num * 3;
* });
* // => { 'a': 3, 'b': 6, 'c': 9 }
*/
function transform(object, callback, accumulator, thisArg) {
var isArr = isArray(object);
if (accumulator == null) {
if (isArr) {
accumulator = [];
} else {
var ctor = object && object.constructor,
proto = ctor && ctor.prototype;
accumulator = baseCreate(proto);
}
}
if (callback) {
callback = lodash.createCallback(callback, thisArg, 4);
(isArr ? forEach : forOwn)(object, function(value, index, object) {
return callback(accumulator, value, index, object);
});
}
return accumulator;
}
/**
* Creates an array composed of the own enumerable property values of `object`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @returns {Array} Returns an array of property values.
* @example
*
* _.values({ 'one': 1, 'two': 2, 'three': 3 });
* // => [1, 2, 3] (property order is not guaranteed across environments)
*/
function values(object) {
var index = -1,
props = keys(object),
length = props.length,
result = Array(length);
while (++index < length) {
result[index] = object[props[index]];
}
return result;
}
/*--------------------------------------------------------------------------*/
/**
* Creates an array of elements from the specified indexes, or keys, of the
* `collection`. Indexes may be specified as individual arguments or as arrays
* of indexes.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {...(number|number[]|string|string[])} [index] The indexes of `collection`
* to retrieve, specified as individual indexes or arrays of indexes.
* @returns {Array} Returns a new array of elements corresponding to the
* provided indexes.
* @example
*
* _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
* // => ['a', 'c', 'e']
*
* _.at(['fred', 'barney', 'pebbles'], 0, 2);
* // => ['fred', 'pebbles']
*/
function at(collection) {
var args = arguments,
index = -1,
props = baseFlatten(args, true, false, 1),
length = (args[2] && args[2][args[1]] === collection) ? 1 : props.length,
result = Array(length);
while(++index < length) {
result[index] = collection[props[index]];
}
return result;
}
/**
* Checks if a given value is present in a collection using strict equality
* for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the
* offset from the end of the collection.
*
* @static
* @memberOf _
* @alias include
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {*} target The value to check for.
* @param {number} [fromIndex=0] The index to search from.
* @returns {boolean} Returns `true` if the `target` element is found, else `false`.
* @example
*
* _.contains([1, 2, 3], 1);
* // => true
*
* _.contains([1, 2, 3], 1, 2);
* // => false
*
* _.contains({ 'name': 'fred', 'age': 40 }, 'fred');
* // => true
*
* _.contains('pebbles', 'eb');
* // => true
*/
function contains(collection, target, fromIndex) {
var index = -1,
indexOf = getIndexOf(),
length = collection ? collection.length : 0,
result = false;
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
if (isArray(collection)) {
result = indexOf(collection, target, fromIndex) > -1;
} else if (typeof length == 'number') {
result = (isString(collection) ? collection.indexOf(target, fromIndex) : indexOf(collection, target, fromIndex)) > -1;
} else {
forOwn(collection, function(value) {
if (++index >= fromIndex) {
return !(result = value === target);
}
});
}
return result;
}
/**
* Creates an object composed of keys generated from the results of running
* each element of `collection` through the callback. The corresponding value
* of each key is the number of times the key was returned by the callback.
* The callback is bound to `thisArg` and invoked with three arguments;
* (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns the composed aggregate object.
* @example
*
* _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
* // => { '4': 1, '6': 2 }
*
* _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
* // => { '4': 1, '6': 2 }
*
* _.countBy(['one', 'two', 'three'], 'length');
* // => { '3': 2, '5': 1 }
*/
var countBy = createAggregator(function(result, value, key) {
(hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
});
/**
* Checks if the given callback returns truey value for **all** elements of
* a collection. The callback is bound to `thisArg` and invoked with three
* arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias all
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {boolean} Returns `true` if all elements passed the callback check,
* else `false`.
* @example
*
* _.every([true, 1, null, 'yes']);
* // => false
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* // using "_.pluck" callback shorthand
* _.every(characters, 'age');
* // => true
*
* // using "_.where" callback shorthand
* _.every(characters, { 'age': 36 });
* // => false
*/
function every(collection, callback, thisArg) {
var result = true;
callback = lodash.createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
while (++index < length) {
if (!(result = !!callback(collection[index], index, collection))) {
break;
}
}
} else {
forOwn(collection, function(value, index, collection) {
return (result = !!callback(value, index, collection));
});
}
return result;
}
/**
* Iterates over elements of a collection, returning an array of all elements
* the callback returns truey for. The callback is bound to `thisArg` and
* invoked with three arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias select
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new array of elements that passed the callback check.
* @example
*
* var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
* // => [2, 4, 6]
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'blocked': false },
* { 'name': 'fred', 'age': 40, 'blocked': true }
* ];
*
* // using "_.pluck" callback shorthand
* _.filter(characters, 'blocked');
* // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
*
* // using "_.where" callback shorthand
* _.filter(characters, { 'age': 36 });
* // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
*/
function filter(collection, callback, thisArg) {
var result = [];
callback = lodash.createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
while (++index < length) {
var value = collection[index];
if (callback(value, index, collection)) {
result.push(value);
}
}
} else {
forOwn(collection, function(value, index, collection) {
if (callback(value, index, collection)) {
result.push(value);
}
});
}
return result;
}
/**
* Iterates over elements of a collection, returning the first element that
* the callback returns truey for. The callback is bound to `thisArg` and
* invoked with three arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias detect, findWhere
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the found element, else `undefined`.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'blocked': false },
* { 'name': 'fred', 'age': 40, 'blocked': true },
* { 'name': 'pebbles', 'age': 1, 'blocked': false }
* ];
*
* _.find(characters, function(chr) {
* return chr.age < 40;
* });
* // => { 'name': 'barney', 'age': 36, 'blocked': false }
*
* // using "_.where" callback shorthand
* _.find(characters, { 'age': 1 });
* // => { 'name': 'pebbles', 'age': 1, 'blocked': false }
*
* // using "_.pluck" callback shorthand
* _.find(characters, 'blocked');
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
*/
function find(collection, callback, thisArg) {
callback = lodash.createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
while (++index < length) {
var value = collection[index];
if (callback(value, index, collection)) {
return value;
}
}
} else {
var result;
forOwn(collection, function(value, index, collection) {
if (callback(value, index, collection)) {
result = value;
return false;
}
});
return result;
}
}
/**
* This method is like `_.find` except that it iterates over elements
* of a `collection` from right to left.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the found element, else `undefined`.
* @example
*
* _.findLast([1, 2, 3, 4], function(num) {
* return num % 2 == 1;
* });
* // => 3
*/
function findLast(collection, callback, thisArg) {
var result;
callback = lodash.createCallback(callback, thisArg, 3);
forEachRight(collection, function(value, index, collection) {
if (callback(value, index, collection)) {
result = value;
return false;
}
});
return result;
}
/**
* Iterates over elements of a collection, executing the callback for each
* element. The callback is bound to `thisArg` and invoked with three arguments;
* (value, index|key, collection). Callbacks may exit iteration early by
* explicitly returning `false`.
*
* Note: As with other "Collections" methods, objects with a `length` property
* are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
* may be used for object iteration.
*
* @static
* @memberOf _
* @alias each
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array|Object|string} Returns `collection`.
* @example
*
* _([1, 2, 3]).forEach(function(num) { console.log(num); }).join(',');
* // => logs each number and returns '1,2,3'
*
* _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); });
* // => logs each number and returns the object (property order is not guaranteed across environments)
*/
function forEach(collection, callback, thisArg) {
var index = -1,
length = collection ? collection.length : 0;
callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
Eif (typeof length == 'number') {
while (++index < length) {
Iif (callback(collection[index], index, collection) === false) {
break;
}
}
} else {
forOwn(collection, callback);
}
return collection;
}
/**
* This method is like `_.forEach` except that it iterates over elements
* of a `collection` from right to left.
*
* @static
* @memberOf _
* @alias eachRight
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array|Object|string} Returns `collection`.
* @example
*
* _([1, 2, 3]).forEachRight(function(num) { console.log(num); }).join(',');
* // => logs each number from right to left and returns '3,2,1'
*/
function forEachRight(collection, callback, thisArg) {
var length = collection ? collection.length : 0;
callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
if (typeof length == 'number') {
while (length--) {
if (callback(collection[length], length, collection) === false) {
break;
}
}
} else {
var props = keys(collection);
length = props.length;
forOwn(collection, function(value, key, collection) {
key = props ? props[--length] : --length;
return callback(collection[key], key, collection);
});
}
return collection;
}
/**
* Creates an object composed of keys generated from the results of running
* each element of a collection through the callback. The corresponding value
* of each key is an array of the elements responsible for generating the key.
* The callback is bound to `thisArg` and invoked with three arguments;
* (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns the composed aggregate object.
* @example
*
* _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
* // => { '4': [4.2], '6': [6.1, 6.4] }
*
* _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
* // => { '4': [4.2], '6': [6.1, 6.4] }
*
* // using "_.pluck" callback shorthand
* _.groupBy(['one', 'two', 'three'], 'length');
* // => { '3': ['one', 'two'], '5': ['three'] }
*/
var groupBy = createAggregator(function(result, value, key) {
(hasOwnProperty.call(result, key) ? result[key] : result[key] = []).push(value);
});
/**
* Creates an object composed of keys generated from the results of running
* each element of the collection through the given callback. The corresponding
* value of each key is the last element responsible for generating the key.
* The callback is bound to `thisArg` and invoked with three arguments;
* (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns the composed aggregate object.
* @example
*
* var keys = [
* { 'dir': 'left', 'code': 97 },
* { 'dir': 'right', 'code': 100 }
* ];
*
* _.indexBy(keys, 'dir');
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
*
* _.indexBy(keys, function(key) { return String.fromCharCode(key.code); });
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
*
* _.indexBy(characters, function(key) { this.fromCharCode(key.code); }, String);
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
*/
var indexBy = createAggregator(function(result, value, key) {
result[key] = value;
});
/**
* Invokes the method named by `methodName` on each element in the `collection`
* returning an array of the results of each invoked method. Additional arguments
* will be provided to each invoked method. If `methodName` is a function it
* will be invoked for, and `this` bound to, each element in the `collection`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|string} methodName The name of the method to invoke or
* the function invoked per iteration.
* @param {...*} [arg] Arguments to invoke the method with.
* @returns {Array} Returns a new array of the results of each invoked method.
* @example
*
* _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
* // => [[1, 5, 7], [1, 2, 3]]
*
* _.invoke([123, 456], String.prototype.split, '');
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
forEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
return result;
}
/**
* Creates an array of values by running each element in the collection
* through the callback. The callback is bound to `thisArg` and invoked with
* three arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias collect
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new array of the results of each `callback` execution.
* @example
*
* _.map([1, 2, 3], function(num) { return num * 3; });
* // => [3, 6, 9]
*
* _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
* // => [3, 6, 9] (property order is not guaranteed across environments)
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* // using "_.pluck" callback shorthand
* _.map(characters, 'name');
* // => ['barney', 'fred']
*/
function map(collection, callback, thisArg) {
var index = -1,
length = collection ? collection.length : 0;
callback = lodash.createCallback(callback, thisArg, 3);
if (typeof length == 'number') {
var result = Array(length);
while (++index < length) {
result[index] = callback(collection[index], index, collection);
}
} else {
result = [];
forOwn(collection, function(value, key, collection) {
result[++index] = callback(value, key, collection);
});
}
return result;
}
/**
* Retrieves the maximum value of a collection. If the collection is empty or
* falsey `-Infinity` is returned. If a callback is provided it will be executed
* for each value in the collection to generate the criterion by which the value
* is ranked. The callback is bound to `thisArg` and invoked with three
* arguments; (value, index, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the maximum value.
* @example
*
* _.max([4, 2, 8, 6]);
* // => 8
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* _.max(characters, function(chr) { return chr.age; });
* // => { 'name': 'fred', 'age': 40 };
*
* // using "_.pluck" callback shorthand
* _.max(characters, 'age');
* // => { 'name': 'fred', 'age': 40 };
*/
function max(collection, callback, thisArg) {
var computed = -Infinity,
result = computed;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
callback = null;
}
if (callback == null && isArray(collection)) {
var index = -1,
length = collection.length;
while (++index < length) {
var value = collection[index];
if (value > result) {
result = value;
}
}
} else {
callback = (callback == null && isString(collection))
? charAtCallback
: lodash.createCallback(callback, thisArg, 3);
forEach(collection, function(value, index, collection) {
var current = callback(value, index, collection);
if (current > computed) {
computed = current;
result = value;
}
});
}
return result;
}
/**
* Retrieves the minimum value of a collection. If the collection is empty or
* falsey `Infinity` is returned. If a callback is provided it will be executed
* for each value in the collection to generate the criterion by which the value
* is ranked. The callback is bound to `thisArg` and invoked with three
* arguments; (value, index, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the minimum value.
* @example
*
* _.min([4, 2, 8, 6]);
* // => 2
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* _.min(characters, function(chr) { return chr.age; });
* // => { 'name': 'barney', 'age': 36 };
*
* // using "_.pluck" callback shorthand
* _.min(characters, 'age');
* // => { 'name': 'barney', 'age': 36 };
*/
function min(collection, callback, thisArg) {
var computed = Infinity,
result = computed;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
callback = null;
}
if (callback == null && isArray(collection)) {
var index = -1,
length = collection.length;
while (++index < length) {
var value = collection[index];
if (value < result) {
result = value;
}
}
} else {
callback = (callback == null && isString(collection))
? charAtCallback
: lodash.createCallback(callback, thisArg, 3);
forEach(collection, function(value, index, collection) {
var current = callback(value, index, collection);
if (current < computed) {
computed = current;
result = value;
}
});
}
return result;
}
/**
* Retrieves the value of a specified property from all elements in the collection.
*
* @static
* @memberOf _
* @type Function
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {string} property The name of the property to pluck.
* @returns {Array} Returns a new array of property values.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* _.pluck(characters, 'name');
* // => ['barney', 'fred']
*/
var pluck = map;
/**
* Reduces a collection to a value which is the accumulated result of running
* each element in the collection through the callback, where each successive
* callback execution consumes the return value of the previous execution. If
* `accumulator` is not provided the first element of the collection will be
* used as the initial `accumulator` value. The callback is bound to `thisArg`
* and invoked with four arguments; (accumulator, value, index|key, collection).
*
* @static
* @memberOf _
* @alias foldl, inject
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [accumulator] Initial value of the accumulator.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the accumulated value.
* @example
*
* var sum = _.reduce([1, 2, 3], function(sum, num) {
* return sum + num;
* });
* // => 6
*
* var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
* result[key] = num * 3;
* return result;
* }, {});
* // => { 'a': 3, 'b': 6, 'c': 9 }
*/
function reduce(collection, callback, accumulator, thisArg) {
if (!collection) return accumulator;
var noaccum = arguments.length < 3;
callback = lodash.createCallback(callback, thisArg, 4);
var index = -1,
length = collection.length;
if (typeof length == 'number') {
if (noaccum) {
accumulator = collection[++index];
}
while (++index < length) {
accumulator = callback(accumulator, collection[index], index, collection);
}
} else {
forOwn(collection, function(value, index, collection) {
accumulator = noaccum
? (noaccum = false, value)
: callback(accumulator, value, index, collection)
});
}
return accumulator;
}
/**
* This method is like `_.reduce` except that it iterates over elements
* of a `collection` from right to left.
*
* @static
* @memberOf _
* @alias foldr
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [accumulator] Initial value of the accumulator.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the accumulated value.
* @example
*
* var list = [[0, 1], [2, 3], [4, 5]];
* var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
* // => [4, 5, 2, 3, 0, 1]
*/
function reduceRight(collection, callback, accumulator, thisArg) {
var noaccum = arguments.length < 3;
callback = lodash.createCallback(callback, thisArg, 4);
forEachRight(collection, function(value, index, collection) {
accumulator = noaccum
? (noaccum = false, value)
: callback(accumulator, value, index, collection);
});
return accumulator;
}
/**
* The opposite of `_.filter` this method returns the elements of a
* collection that the callback does **not** return truey for.
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new array of elements that failed the callback check.
* @example
*
* var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
* // => [1, 3, 5]
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'blocked': false },
* { 'name': 'fred', 'age': 40, 'blocked': true }
* ];
*
* // using "_.pluck" callback shorthand
* _.reject(characters, 'blocked');
* // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
*
* // using "_.where" callback shorthand
* _.reject(characters, { 'age': 36 });
* // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
*/
function reject(collection, callback, thisArg) {
callback = lodash.createCallback(callback, thisArg, 3);
return filter(collection, function(value, index, collection) {
return !callback(value, index, collection);
});
}
/**
* Retrieves a random element or `n` random elements from a collection.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to sample.
* @param {number} [n] The number of elements to sample.
* @param- {Object} [guard] Allows working with functions like `_.map`
* without using their `index` arguments as `n`.
* @returns {Array} Returns the random sample(s) of `collection`.
* @example
*
* _.sample([1, 2, 3, 4]);
* // => 2
*
* _.sample([1, 2, 3, 4], 2);
* // => [3, 1]
*/
function sample(collection, n, guard) {
if (collection && typeof collection.length != 'number') {
collection = values(collection);
}
if (n == null || guard) {
return collection ? collection[baseRandom(0, collection.length - 1)] : undefined;
}
var result = shuffle(collection);
result.length = nativeMin(nativeMax(0, n), result.length);
return result;
}
/**
* Creates an array of shuffled values, using a version of the Fisher-Yates
* shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to shuffle.
* @returns {Array} Returns a new shuffled collection.
* @example
*
* _.shuffle([1, 2, 3, 4, 5, 6]);
* // => [4, 1, 6, 3, 5, 2]
*/
function shuffle(collection) {
var index = -1,
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
forEach(collection, function(value) {
var rand = baseRandom(0, ++index);
result[index] = result[rand];
result[rand] = value;
});
return result;
}
/**
* Gets the size of the `collection` by returning `collection.length` for arrays
* and array-like objects or the number of own enumerable properties for objects.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to inspect.
* @returns {number} Returns `collection.length` or number of own enumerable properties.
* @example
*
* _.size([1, 2]);
* // => 2
*
* _.size({ 'one': 1, 'two': 2, 'three': 3 });
* // => 3
*
* _.size('pebbles');
* // => 7
*/
function size(collection) {
var length = collection ? collection.length : 0;
return typeof length == 'number' ? length : keys(collection).length;
}
/**
* Checks if the callback returns a truey value for **any** element of a
* collection. The function returns as soon as it finds a passing value and
* does not iterate over the entire collection. The callback is bound to
* `thisArg` and invoked with three arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias any
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {boolean} Returns `true` if any element passed the callback check,
* else `false`.
* @example
*
* _.some([null, 0, 'yes', false], Boolean);
* // => true
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'blocked': false },
* { 'name': 'fred', 'age': 40, 'blocked': true }
* ];
*
* // using "_.pluck" callback shorthand
* _.some(characters, 'blocked');
* // => true
*
* // using "_.where" callback shorthand
* _.some(characters, { 'age': 1 });
* // => false
*/
function some(collection, callback, thisArg) {
var result;
callback = lodash.createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
while (++index < length) {
if ((result = callback(collection[index], index, collection))) {
break;
}
}
} else {
forOwn(collection, function(value, index, collection) {
return !(result = callback(value, index, collection));
});
}
return !!result;
}
/**
* Creates an array of elements, sorted in ascending order by the results of
* running each element in a collection through the callback. This method
* performs a stable sort, that is, it will preserve the original sort order
* of equal elements. The callback is bound to `thisArg` and invoked with
* three arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an array of property names is provided for `callback` the collection
* will be sorted by each property value.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Array|Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new array of sorted elements.
* @example
*
* _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
* // => [3, 1, 2]
*
* _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
* // => [3, 1, 2]
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 },
* { 'name': 'barney', 'age': 26 },
* { 'name': 'fred', 'age': 30 }
* ];
*
* // using "_.pluck" callback shorthand
* _.map(_.sortBy(characters, 'age'), _.values);
* // => [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]]
*
* // sorting by multiple properties
* _.map(_.sortBy(characters, ['name', 'age']), _.values);
* // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
*/
function sortBy(collection, callback, thisArg) {
var index = -1,
isArr = isArray(callback),
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
if (!isArr) {
callback = lodash.createCallback(callback, thisArg, 3);
}
forEach(collection, function(value, key, collection) {
var object = result[++index] = getObject();
if (isArr) {
object.criteria = map(callback, function(key) { return value[key]; });
} else {
(object.criteria = getArray())[0] = callback(value, key, collection);
}
object.index = index;
object.value = value;
});
length = result.length;
result.sort(compareAscending);
while (length--) {
var object = result[length];
result[length] = object.value;
if (!isArr) {
releaseArray(object.criteria);
}
releaseObject(object);
}
return result;
}
/**
* Converts the `collection` to an array.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to convert.
* @returns {Array} Returns the new converted array.
* @example
*
* (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
* // => [2, 3, 4]
*/
function toArray(collection) {
if (collection && typeof collection.length == 'number') {
return slice(collection);
}
return values(collection);
}
/**
* Performs a deep comparison of each element in a `collection` to the given
* `properties` object, returning an array of all elements that have equivalent
* property values.
*
* @static
* @memberOf _
* @type Function
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Object} props The object of property values to filter by.
* @returns {Array} Returns a new array of elements that have the given properties.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'pets': ['hoppy'] },
* { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
* ];
*
* _.where(characters, { 'age': 36 });
* // => [{ 'name': 'barney', 'age': 36, 'pets': ['hoppy'] }]
*
* _.where(characters, { 'pets': ['dino'] });
* // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
*/
var where = filter;
/*--------------------------------------------------------------------------*/
/**
* Creates an array with all falsey values removed. The values `false`, `null`,
* `0`, `""`, `undefined`, and `NaN` are all falsey.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to compact.
* @returns {Array} Returns a new array of filtered values.
* @example
*
* _.compact([0, 1, false, 2, '', 3]);
* // => [1, 2, 3]
*/
function compact(array) {
var index = -1,
length = array ? array.length : 0,
result = [];
while (++index < length) {
var value = array[index];
if (value) {
result.push(value);
}
}
return result;
}
/**
* Creates an array excluding all values of the provided arrays using strict
* equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to process.
* @param {...Array} [values] The arrays of values to exclude.
* @returns {Array} Returns a new array of filtered values.
* @example
*
* _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
* // => [1, 3, 4]
*/
function difference(array) {
return baseDifference(array, baseFlatten(arguments, true, true, 1));
}
/**
* This method is like `_.find` except that it returns the index of the first
* element that passes the callback check, instead of the element itself.
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to search.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {number} Returns the index of the found element, else `-1`.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'blocked': false },
* { 'name': 'fred', 'age': 40, 'blocked': true },
* { 'name': 'pebbles', 'age': 1, 'blocked': false }
* ];
*
* _.findIndex(characters, function(chr) {
* return chr.age < 20;
* });
* // => 2
*
* // using "_.where" callback shorthand
* _.findIndex(characters, { 'age': 36 });
* // => 0
*
* // using "_.pluck" callback shorthand
* _.findIndex(characters, 'blocked');
* // => 1
*/
function findIndex(array, callback, thisArg) {
var index = -1,
length = array ? array.length : 0;
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length) {
if (callback(array[index], index, array)) {
return index;
}
}
return -1;
}
/**
* This method is like `_.findIndex` except that it iterates over elements
* of a `collection` from right to left.
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to search.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {number} Returns the index of the found element, else `-1`.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36, 'blocked': true },
* { 'name': 'fred', 'age': 40, 'blocked': false },
* { 'name': 'pebbles', 'age': 1, 'blocked': true }
* ];
*
* _.findLastIndex(characters, function(chr) {
* return chr.age > 30;
* });
* // => 1
*
* // using "_.where" callback shorthand
* _.findLastIndex(characters, { 'age': 36 });
* // => 0
*
* // using "_.pluck" callback shorthand
* _.findLastIndex(characters, 'blocked');
* // => 2
*/
function findLastIndex(array, callback, thisArg) {
var length = array ? array.length : 0;
callback = lodash.createCallback(callback, thisArg, 3);
while (length--) {
if (callback(array[length], length, array)) {
return length;
}
}
return -1;
}
/**
* Gets the first element or first `n` elements of an array. If a callback
* is provided elements at the beginning of the array are returned as long
* as the callback returns truey. The callback is bound to `thisArg` and
* invoked with three arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias head, take
* @category Arrays
* @param {Array} array The array to query.
* @param {Function|Object|number|string} [callback] The function called
* per element or the number of elements to return. If a property name or
* object is provided it will be used to create a "_.pluck" or "_.where"
* style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the first element(s) of `array`.
* @example
*
* _.first([1, 2, 3]);
* // => 1
*
* _.first([1, 2, 3], 2);
* // => [1, 2]
*
* _.first([1, 2, 3], function(num) {
* return num < 3;
* });
* // => [1, 2]
*
* var characters = [
* { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
* { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
* { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
* ];
*
* // using "_.pluck" callback shorthand
* _.first(characters, 'blocked');
* // => [{ 'name': 'barney', 'blocked': true, 'employer': 'slate' }]
*
* // using "_.where" callback shorthand
* _.pluck(_.first(characters, { 'employer': 'slate' }), 'name');
* // => ['barney', 'fred']
*/
function first(array, callback, thisArg) {
var n = 0,
length = array ? array.length : 0;
if (typeof callback != 'number' && callback != null) {
var index = -1;
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length && callback(array[index], index, array)) {
n++;
}
} else {
n = callback;
if (n == null || thisArg) {
return array ? array[0] : undefined;
}
}
return slice(array, 0, nativeMin(nativeMax(0, n), length));
}
/**
* Flattens a nested array (the nesting can be to any depth). If `isShallow`
* is truey, the array will only be flattened a single level. If a callback
* is provided each element of the array is passed through the callback before
* flattening. The callback is bound to `thisArg` and invoked with three
* arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to flatten.
* @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new flattened array.
* @example
*
* _.flatten([1, [2], [3, [[4]]]]);
* // => [1, 2, 3, 4];
*
* _.flatten([1, [2], [3, [[4]]]], true);
* // => [1, 2, 3, [[4]]];
*
* var characters = [
* { 'name': 'barney', 'age': 30, 'pets': ['hoppy'] },
* { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
* ];
*
* // using "_.pluck" callback shorthand
* _.flatten(characters, 'pets');
* // => ['hoppy', 'baby puss', 'dino']
*/
function flatten(array, isShallow, callback, thisArg) {
// juggle arguments
if (typeof isShallow != 'boolean' && isShallow != null) {
thisArg = callback;
callback = (typeof isShallow != 'function' && thisArg && thisArg[isShallow] === array) ? null : isShallow;
isShallow = false;
}
if (callback != null) {
array = map(array, callback, thisArg);
}
return baseFlatten(array, isShallow);
}
/**
* Gets the index at which the first occurrence of `value` is found using
* strict equality for comparisons, i.e. `===`. If the array is already sorted
* providing `true` for `fromIndex` will run a faster binary search.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to search.
* @param {*} value The value to search for.
* @param {boolean|number} [fromIndex=0] The index to search from or `true`
* to perform a binary search on a sorted array.
* @returns {number} Returns the index of the matched value or `-1`.
* @example
*
* _.indexOf([1, 2, 3, 1, 2, 3], 2);
* // => 1
*
* _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
* // => 4
*
* _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
* // => 2
*/
function indexOf(array, value, fromIndex) {
if (typeof fromIndex == 'number') {
var length = array ? array.length : 0;
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0);
} else if (fromIndex) {
var index = sortedIndex(array, value);
return array[index] === value ? index : -1;
}
return baseIndexOf(array, value, fromIndex);
}
/**
* Gets all but the last element or last `n` elements of an array. If a
* callback is provided elements at the end of the array are excluded from
* the result as long as the callback returns truey. The callback is bound
* to `thisArg` and invoked with three arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to query.
* @param {Function|Object|number|string} [callback=1] The function called
* per element or the number of elements to exclude. If a property name or
* object is provided it will be used to create a "_.pluck" or "_.where"
* style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a slice of `array`.
* @example
*
* _.initial([1, 2, 3]);
* // => [1, 2]
*
* _.initial([1, 2, 3], 2);
* // => [1]
*
* _.initial([1, 2, 3], function(num) {
* return num > 1;
* });
* // => [1]
*
* var characters = [
* { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
* { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
* { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
* ];
*
* // using "_.pluck" callback shorthand
* _.initial(characters, 'blocked');
* // => [{ 'name': 'barney', 'blocked': false, 'employer': 'slate' }]
*
* // using "_.where" callback shorthand
* _.pluck(_.initial(characters, { 'employer': 'na' }), 'name');
* // => ['barney', 'fred']
*/
function initial(array, callback, thisArg) {
var n = 0,
length = array ? array.length : 0;
if (typeof callback != 'number' && callback != null) {
var index = length;
callback = lodash.createCallback(callback, thisArg, 3);
while (index-- && callback(array[index], index, array)) {
n++;
}
} else {
n = (callback == null || thisArg) ? 1 : callback || n;
}
return slice(array, 0, nativeMin(nativeMax(0, length - n), length));
}
/**
* Creates an array of unique values present in all provided arrays using
* strict equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {...Array} [array] The arrays to inspect.
* @returns {Array} Returns an array of shared values.
* @example
*
* _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
* // => [1, 2]
*/
function intersection() {
var args = [],
argsIndex = -1,
argsLength = arguments.length,
caches = getArray(),
indexOf = getIndexOf(),
trustIndexOf = indexOf === baseIndexOf,
seen = getArray();
while (++argsIndex < argsLength) {
var value = arguments[argsIndex];
if (isArray(value) || isArguments(value)) {
args.push(value);
caches.push(trustIndexOf && value.length >= largeArraySize &&
createCache(argsIndex ? args[argsIndex] : seen));
}
}
var array = args[0],
index = -1,
length = array ? array.length : 0,
result = [];
outer:
while (++index < length) {
var cache = caches[0];
value = array[index];
if ((cache ? cacheIndexOf(cache, value) : indexOf(seen, value)) < 0) {
argsIndex = argsLength;
(cache || seen).push(value);
while (--argsIndex) {
cache = caches[argsIndex];
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) {
continue outer;
}
}
result.push(value);
}
}
while (argsLength--) {
cache = caches[argsLength];
if (cache) {
releaseObject(cache);
}
}
releaseArray(caches);
releaseArray(seen);
return result;
}
/**
* Gets the last element or last `n` elements of an array. If a callback is
* provided elements at the end of the array are returned as long as the
* callback returns truey. The callback is bound to `thisArg` and invoked
* with three arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to query.
* @param {Function|Object|number|string} [callback] The function called
* per element or the number of elements to return. If a property name or
* object is provided it will be used to create a "_.pluck" or "_.where"
* style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the last element(s) of `array`.
* @example
*
* _.last([1, 2, 3]);
* // => 3
*
* _.last([1, 2, 3], 2);
* // => [2, 3]
*
* _.last([1, 2, 3], function(num) {
* return num > 1;
* });
* // => [2, 3]
*
* var characters = [
* { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
* { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
* { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
* ];
*
* // using "_.pluck" callback shorthand
* _.pluck(_.last(characters, 'blocked'), 'name');
* // => ['fred', 'pebbles']
*
* // using "_.where" callback shorthand
* _.last(characters, { 'employer': 'na' });
* // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
*/
function last(array, callback, thisArg) {
var n = 0,
length = array ? array.length : 0;
if (typeof callback != 'number' && callback != null) {
var index = length;
callback = lodash.createCallback(callback, thisArg, 3);
while (index-- && callback(array[index], index, array)) {
n++;
}
} else {
n = callback;
if (n == null || thisArg) {
return array ? array[length - 1] : undefined;
}
}
return slice(array, nativeMax(0, length - n));
}
/**
* Gets the index at which the last occurrence of `value` is found using strict
* equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
* as the offset from the end of the collection.
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to search.
* @param {*} value The value to search for.
* @param {number} [fromIndex=array.length-1] The index to search from.
* @returns {number} Returns the index of the matched value or `-1`.
* @example
*
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
* // => 4
*
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
* // => 1
*/
function lastIndexOf(array, value, fromIndex) {
var index = array ? array.length : 0;
if (typeof fromIndex == 'number') {
index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
}
while (index--) {
if (array[index] === value) {
return index;
}
}
return -1;
}
/**
* Removes all provided values from the given array using strict equality for
* comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to modify.
* @param {...*} [value] The values to remove.
* @returns {Array} Returns `array`.
* @example
*
* var array = [1, 2, 3, 1, 2, 3];
* _.pull(array, 2, 3);
* console.log(array);
* // => [1, 1]
*/
function pull(array) {
var args = arguments,
argsIndex = 0,
argsLength = args.length,
length = array ? array.length : 0;
while (++argsIndex < argsLength) {
var index = -1,
value = args[argsIndex];
while (++index < length) {
if (array[index] === value) {
splice.call(array, index--, 1);
length--;
}
}
}
return array;
}
/**
* Creates an array of numbers (positive and/or negative) progressing from
* `start` up to but not including `end`. If `start` is less than `stop` a
* zero-length range is created unless a negative `step` is specified.
*
* @static
* @memberOf _
* @category Arrays
* @param {number} [start=0] The start of the range.
* @param {number} end The end of the range.
* @param {number} [step=1] The value to increment or decrement by.
* @returns {Array} Returns a new range array.
* @example
*
* _.range(4);
* // => [0, 1, 2, 3]
*
* _.range(1, 5);
* // => [1, 2, 3, 4]
*
* _.range(0, 20, 5);
* // => [0, 5, 10, 15]
*
* _.range(0, -4, -1);
* // => [0, -1, -2, -3]
*
* _.range(1, 4, 0);
* // => [1, 1, 1]
*
* _.range(0);
* // => []
*/
function range(start, end, step) {
start = +start || 0;
step = typeof step == 'number' ? step : (+step || 1);
if (end == null) {
end = start;
start = 0;
}
// use `Array(length)` so engines like Chakra and V8 avoid slower modes
// http://youtu.be/XAqIpGU8ZZk#t=17m25s
var index = -1,
length = nativeMax(0, ceil((end - start) / (step || 1))),
result = Array(length);
while (++index < length) {
result[index] = start;
start += step;
}
return result;
}
/**
* Removes all elements from an array that the callback returns truey for
* and returns an array of removed elements. The callback is bound to `thisArg`
* and invoked with three arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to modify.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new array of removed elements.
* @example
*
* var array = [1, 2, 3, 4, 5, 6];
* var evens = _.remove(array, function(num) { return num % 2 == 0; });
*
* console.log(array);
* // => [1, 3, 5]
*
* console.log(evens);
* // => [2, 4, 6]
*/
function remove(array, callback, thisArg) {
var index = -1,
length = array ? array.length : 0,
result = [];
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length) {
var value = array[index];
if (callback(value, index, array)) {
result.push(value);
splice.call(array, index--, 1);
length--;
}
}
return result;
}
/**
* The opposite of `_.initial` this method gets all but the first element or
* first `n` elements of an array. If a callback function is provided elements
* at the beginning of the array are excluded from the result as long as the
* callback returns truey. The callback is bound to `thisArg` and invoked
* with three arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias drop, tail
* @category Arrays
* @param {Array} array The array to query.
* @param {Function|Object|number|string} [callback=1] The function called
* per element or the number of elements to exclude. If a property name or
* object is provided it will be used to create a "_.pluck" or "_.where"
* style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a slice of `array`.
* @example
*
* _.rest([1, 2, 3]);
* // => [2, 3]
*
* _.rest([1, 2, 3], 2);
* // => [3]
*
* _.rest([1, 2, 3], function(num) {
* return num < 3;
* });
* // => [3]
*
* var characters = [
* { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
* { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
* { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
* ];
*
* // using "_.pluck" callback shorthand
* _.pluck(_.rest(characters, 'blocked'), 'name');
* // => ['fred', 'pebbles']
*
* // using "_.where" callback shorthand
* _.rest(characters, { 'employer': 'slate' });
* // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
*/
function rest(array, callback, thisArg) {
if (typeof callback != 'number' && callback != null) {
var n = 0,
index = -1,
length = array ? array.length : 0;
callback = lodash.createCallback(callback, thisArg, 3);
while (++index < length && callback(array[index], index, array)) {
n++;
}
} else {
n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
}
return slice(array, n);
}
/**
* Uses a binary search to determine the smallest index at which a value
* should be inserted into a given sorted array in order to maintain the sort
* order of the array. If a callback is provided it will be executed for
* `value` and each element of `array` to compute their sort ranking. The
* callback is bound to `thisArg` and invoked with one argument; (value).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to inspect.
* @param {*} value The value to evaluate.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {number} Returns the index at which `value` should be inserted
* into `array`.
* @example
*
* _.sortedIndex([20, 30, 50], 40);
* // => 2
*
* // using "_.pluck" callback shorthand
* _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
* // => 2
*
* var dict = {
* 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }
* };
*
* _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
* return dict.wordToNumber[word];
* });
* // => 2
*
* _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
* return this.wordToNumber[word];
* }, dict);
* // => 2
*/
function sortedIndex(array, value, callback, thisArg) {
var low = 0,
high = array ? array.length : low;
// explicitly reference `identity` for better inlining in Firefox
callback = callback ? lodash.createCallback(callback, thisArg, 1) : identity;
value = callback(value);
while (low < high) {
var mid = (low + high) >>> 1;
(callback(array[mid]) < value)
? low = mid + 1
: high = mid;
}
return low;
}
/**
* Creates an array of unique values, in order, of the provided arrays using
* strict equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {...Array} [array] The arrays to inspect.
* @returns {Array} Returns an array of combined values.
* @example
*
* _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]);
* // => [1, 2, 3, 5, 4]
*/
function union() {
return baseUniq(baseFlatten(arguments, true, true));
}
/**
* Creates a duplicate-value-free version of an array using strict equality
* for comparisons, i.e. `===`. If the array is sorted, providing
* `true` for `isSorted` will use a faster algorithm. If a callback is provided
* each element of `array` is passed through the callback before uniqueness
* is computed. The callback is bound to `thisArg` and invoked with three
* arguments; (value, index, array).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @alias unique
* @category Arrays
* @param {Array} array The array to process.
* @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a duplicate-value-free array.
* @example
*
* _.uniq([1, 2, 1, 3, 1]);
* // => [1, 2, 3]
*
* _.uniq([1, 1, 2, 2, 3], true);
* // => [1, 2, 3]
*
* _.uniq(['A', 'b', 'C', 'a', 'B', 'c'], function(letter) { return letter.toLowerCase(); });
* // => ['A', 'b', 'C']
*
* _.uniq([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math);
* // => [1, 2.5, 3]
*
* // using "_.pluck" callback shorthand
* _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
* // => [{ 'x': 1 }, { 'x': 2 }]
*/
function uniq(array, isSorted, callback, thisArg) {
// juggle arguments
if (typeof isSorted != 'boolean' && isSorted != null) {
thisArg = callback;
callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted;
isSorted = false;
}
if (callback != null) {
callback = lodash.createCallback(callback, thisArg, 3);
}
return baseUniq(array, isSorted, callback);
}
/**
* Creates an array excluding all provided values using strict equality for
* comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to filter.
* @param {...*} [value] The values to exclude.
* @returns {Array} Returns a new array of filtered values.
* @example
*
* _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
* // => [2, 3, 4]
*/
function without(array) {
return baseDifference(array, slice(arguments, 1));
}
/**
* Creates an array that is the symmetric difference of the provided arrays.
* See http://en.wikipedia.org/wiki/Symmetric_difference.
*
* @static
* @memberOf _
* @category Arrays
* @param {...Array} [array] The arrays to inspect.
* @returns {Array} Returns an array of values.
* @example
*
* _.xor([1, 2, 3], [5, 2, 1, 4]);
* // => [3, 5, 4]
*
* _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
* // => [1, 4, 5]
*/
function xor() {
var index = -1,
length = arguments.length;
while (++index < length) {
var array = arguments[index];
if (isArray(array) || isArguments(array)) {
var result = result
? baseUniq(baseDifference(result, array).concat(baseDifference(array, result)))
: array;
}
}
return result || [];
}
/**
* Creates an array of grouped elements, the first of which contains the first
* elements of the given arrays, the second of which contains the second
* elements of the given arrays, and so on.
*
* @static
* @memberOf _
* @alias unzip
* @category Arrays
* @param {...Array} [array] Arrays to process.
* @returns {Array} Returns a new array of grouped elements.
* @example
*
* _.zip(['fred', 'barney'], [30, 40], [true, false]);
* // => [['fred', 30, true], ['barney', 40, false]]
*/
function zip() {
var array = arguments.length > 1 ? arguments : arguments[0],
index = -1,
length = array ? max(pluck(array, 'length')) : 0,
result = Array(length < 0 ? 0 : length);
while (++index < length) {
result[index] = pluck(array, index);
}
return result;
}
/**
* Creates an object composed from arrays of `keys` and `values`. Provide
* either a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`
* or two arrays, one of `keys` and one of corresponding `values`.
*
* @static
* @memberOf _
* @alias object
* @category Arrays
* @param {Array} keys The array of keys.
* @param {Array} [values=[]] The array of values.
* @returns {Object} Returns an object composed of the given keys and
* corresponding values.
* @example
*
* _.zipObject(['fred', 'barney'], [30, 40]);
* // => { 'fred': 30, 'barney': 40 }
*/
function zipObject(keys, values) {
var index = -1,
length = keys ? keys.length : 0,
result = {};
if (!values && length && !isArray(keys[0])) {
values = [];
}
while (++index < length) {
var key = keys[index];
if (values) {
result[key] = values[index];
} else if (key) {
result[key[0]] = key[1];
}
}
return result;
}
/*--------------------------------------------------------------------------*/
/**
* Creates a function that executes `func`, with the `this` binding and
* arguments of the created function, only after being called `n` times.
*
* @static
* @memberOf _
* @category Functions
* @param {number} n The number of times the function must be called before
* `func` is executed.
* @param {Function} func The function to restrict.
* @returns {Function} Returns the new restricted function.
* @example
*
* var saves = ['profile', 'settings'];
*
* var done = _.after(saves.length, function() {
* console.log('Done saving!');
* });
*
* _.forEach(saves, function(type) {
* asyncSave({ 'type': type, 'complete': done });
* });
* // => logs 'Done saving!', after all saves have completed
*/
function after(n, func) {
if (!isFunction(func)) {
throw new TypeError;
}
return function() {
if (--n < 1) {
return func.apply(this, arguments);
}
};
}
/**
* Creates a function that, when called, invokes `func` with the `this`
* binding of `thisArg` and prepends any additional `bind` arguments to those
* provided to the bound function.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to bind.
* @param {*} [thisArg] The `this` binding of `func`.
* @param {...*} [arg] Arguments to be partially applied.
* @returns {Function} Returns the new bound function.
* @example
*
* var func = function(greeting) {
* return greeting + ' ' + this.name;
* };
*
* func = _.bind(func, { 'name': 'fred' }, 'hi');
* func();
* // => 'hi fred'
*/
function bind(func, thisArg) {
return arguments.length > 2
? createWrapper(func, 17, slice(arguments, 2), null, thisArg)
: createWrapper(func, 1, null, null, thisArg);
}
/**
* Binds methods of an object to the object itself, overwriting the existing
* method. Method names may be specified as individual arguments or as arrays
* of method names. If no method names are provided all the function properties
* of `object` will be bound.
*
* @static
* @memberOf _
* @category Functions
* @param {Object} object The object to bind and assign the bound methods to.
* @param {...string} [methodName] The object method names to
* bind, specified as individual method names or arrays of method names.
* @returns {Object} Returns `object`.
* @example
*
* var view = {
* 'label': 'docs',
* 'onClick': function() { console.log('clicked ' + this.label); }
* };
*
* _.bindAll(view);
* jQuery('#docs').on('click', view.onClick);
* // => logs 'clicked docs', when the button is clicked
*/
function bindAll(object) {
var funcs = arguments.length > 1 ? baseFlatten(arguments, true, false, 1) : functions(object),
index = -1,
length = funcs.length;
while (++index < length) {
var key = funcs[index];
object[key] = createWrapper(object[key], 1, null, null, object);
}
return object;
}
/**
* Creates a function that, when called, invokes the method at `object[key]`
* and prepends any additional `bindKey` arguments to those provided to the bound
* function. This method differs from `_.bind` by allowing bound functions to
* reference methods that will be redefined or don't yet exist.
* See http://michaux.ca/articles/lazy-function-definition-pattern.
*
* @static
* @memberOf _
* @category Functions
* @param {Object} object The object the method belongs to.
* @param {string} key The key of the method.
* @param {...*} [arg] Arguments to be partially applied.
* @returns {Function} Returns the new bound function.
* @example
*
* var object = {
* 'name': 'fred',
* 'greet': function(greeting) {
* return greeting + ' ' + this.name;
* }
* };
*
* var func = _.bindKey(object, 'greet', 'hi');
* func();
* // => 'hi fred'
*
* object.greet = function(greeting) {
* return greeting + 'ya ' + this.name + '!';
* };
*
* func();
* // => 'hiya fred!'
*/
function bindKey(object, key) {
return arguments.length > 2
? createWrapper(key, 19, slice(arguments, 2), null, object)
: createWrapper(key, 3, null, null, object);
}
/**
* Creates a function that is the composition of the provided functions,
* where each function consumes the return value of the function that follows.
* For example, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`.
* Each function is executed with the `this` binding of the composed function.
*
* @static
* @memberOf _
* @category Functions
* @param {...Function} [func] Functions to compose.
* @returns {Function} Returns the new composed function.
* @example
*
* var realNameMap = {
* 'pebbles': 'penelope'
* };
*
* var format = function(name) {
* name = realNameMap[name.toLowerCase()] || name;
* return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
* };
*
* var greet = function(formatted) {
* return 'Hiya ' + formatted + '!';
* };
*
* var welcome = _.compose(greet, format);
* welcome('pebbles');
* // => 'Hiya Penelope!'
*/
function compose() {
var funcs = arguments,
length = funcs.length;
while (length--) {
if (!isFunction(funcs[length])) {
throw new TypeError;
}
}
return function() {
var args = arguments,
length = funcs.length;
while (length--) {
args = [funcs[length].apply(this, args)];
}
return args[0];
};
}
/**
* Creates a function which accepts one or more arguments of `func` that when
* invoked either executes `func` returning its result, if all `func` arguments
* have been provided, or returns a function that accepts one or more of the
* remaining `func` arguments, and so on. The arity of `func` can be specified
* if `func.length` is not sufficient.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to curry.
* @param {number} [arity=func.length] The arity of `func`.
* @returns {Function} Returns the new curried function.
* @example
*
* var curried = _.curry(function(a, b, c) {
* console.log(a + b + c);
* });
*
* curried(1)(2)(3);
* // => 6
*
* curried(1, 2)(3);
* // => 6
*
* curried(1, 2, 3);
* // => 6
*/
function curry(func, arity) {
arity = typeof arity == 'number' ? arity : (+arity || func.length);
return createWrapper(func, 4, null, null, null, arity);
}
/**
* Creates a function that will delay the execution of `func` until after
* `wait` milliseconds have elapsed since the last time it was invoked.
* Provide an options object to indicate that `func` should be invoked on
* the leading and/or trailing edge of the `wait` timeout. Subsequent calls
* to the debounced function will return the result of the last `func` call.
*
* Note: If `leading` and `trailing` options are `true` `func` will be called
* on the trailing edge of the timeout only if the the debounced function is
* invoked more than once during the `wait` timeout.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to debounce.
* @param {number} wait The number of milliseconds to delay.
* @param {Object} [options] The options object.
* @param {boolean} [options.leading=false] Specify execution on the leading edge of the timeout.
* @param {number} [options.maxWait] The maximum time `func` is allowed to be delayed before it's called.
* @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
* @returns {Function} Returns the new debounced function.
* @example
*
* // avoid costly calculations while the window size is in flux
* var lazyLayout = _.debounce(calculateLayout, 150);
* jQuery(window).on('resize', lazyLayout);
*
* // execute `sendMail` when the click event is fired, debouncing subsequent calls
* jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
* 'leading': true,
* 'trailing': false
* });
*
* // ensure `batchLog` is executed once after 1 second of debounced calls
* var source = new EventSource('/stream');
* source.addEventListener('message', _.debounce(batchLog, 250, {
* 'maxWait': 1000
* }, false);
*/
function debounce(func, wait, options) {
var args,
maxTimeoutId,
result,
stamp,
thisArg,
timeoutId,
trailingCall,
lastCalled = 0,
maxWait = false,
trailing = true;
if (!isFunction(func)) {
throw new TypeError;
}
wait = nativeMax(0, wait) || 0;
if (options === true) {
var leading = true;
trailing = false;
} else if (isObject(options)) {
leading = options.leading;
maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
trailing = 'trailing' in options ? options.trailing : trailing;
}
var delayed = function() {
var remaining = wait - (now() - stamp);
if (remaining <= 0) {
if (maxTimeoutId) {
clearTimeout(maxTimeoutId);
}
var isCalled = trailingCall;
maxTimeoutId = timeoutId = trailingCall = undefined;
if (isCalled) {
lastCalled = now();
result = func.apply(thisArg, args);
if (!timeoutId && !maxTimeoutId) {
args = thisArg = null;
}
}
} else {
timeoutId = setTimeout(delayed, remaining);
}
};
var maxDelayed = function() {
if (timeoutId) {
clearTimeout(timeoutId);
}
maxTimeoutId = timeoutId = trailingCall = undefined;
if (trailing || (maxWait !== wait)) {
lastCalled = now();
result = func.apply(thisArg, args);
if (!timeoutId && !maxTimeoutId) {
args = thisArg = null;
}
}
};
return function() {
args = arguments;
stamp = now();
thisArg = this;
trailingCall = trailing && (timeoutId || !leading);
if (maxWait === false) {
var leadingCall = leading && !timeoutId;
} else {
if (!maxTimeoutId && !leading) {
lastCalled = stamp;
}
var remaining = maxWait - (stamp - lastCalled),
isCalled = remaining <= 0;
if (isCalled) {
if (maxTimeoutId) {
maxTimeoutId = clearTimeout(maxTimeoutId);
}
lastCalled = stamp;
result = func.apply(thisArg, args);
}
else if (!maxTimeoutId) {
maxTimeoutId = setTimeout(maxDelayed, remaining);
}
}
if (isCalled && timeoutId) {
timeoutId = clearTimeout(timeoutId);
}
else if (!timeoutId && wait !== maxWait) {
timeoutId = setTimeout(delayed, wait);
}
if (leadingCall) {
isCalled = true;
result = func.apply(thisArg, args);
}
if (isCalled && !timeoutId && !maxTimeoutId) {
args = thisArg = null;
}
return result;
};
}
/**
* Defers executing the `func` function until the current call stack has cleared.
* Additional arguments will be provided to `func` when it is invoked.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to defer.
* @param {...*} [arg] Arguments to invoke the function with.
* @returns {number} Returns the timer id.
* @example
*
* _.defer(function(text) { console.log(text); }, 'deferred');
* // logs 'deferred' after one or more milliseconds
*/
function defer(func) {
if (!isFunction(func)) {
throw new TypeError;
}
var args = slice(arguments, 1);
return setTimeout(function() { func.apply(undefined, args); }, 1);
}
/**
* Executes the `func` function after `wait` milliseconds. Additional arguments
* will be provided to `func` when it is invoked.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to delay.
* @param {number} wait The number of milliseconds to delay execution.
* @param {...*} [arg] Arguments to invoke the function with.
* @returns {number} Returns the timer id.
* @example
*
* _.delay(function(text) { console.log(text); }, 1000, 'later');
* // => logs 'later' after one second
*/
function delay(func, wait) {
if (!isFunction(func)) {
throw new TypeError;
}
var args = slice(arguments, 2);
return setTimeout(function() { func.apply(undefined, args); }, wait);
}
/**
* Creates a function that memoizes the result of `func`. If `resolver` is
* provided it will be used to determine the cache key for storing the result
* based on the arguments provided to the memoized function. By default, the
* first argument provided to the memoized function is used as the cache key.
* The `func` is executed with the `this` binding of the memoized function.
* The result cache is exposed as the `cache` property on the memoized function.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to have its output memoized.
* @param {Function} [resolver] A function used to resolve the cache key.
* @returns {Function} Returns the new memoizing function.
* @example
*
* var fibonacci = _.memoize(function(n) {
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
* });
*
* fibonacci(9)
* // => 34
*
* var data = {
* 'fred': { 'name': 'fred', 'age': 40 },
* 'pebbles': { 'name': 'pebbles', 'age': 1 }
* };
*
* // modifying the result cache
* var get = _.memoize(function(name) { return data[name]; }, _.identity);
* get('pebbles');
* // => { 'name': 'pebbles', 'age': 1 }
*
* get.cache.pebbles.name = 'penelope';
* get('pebbles');
* // => { 'name': 'penelope', 'age': 1 }
*/
function memoize(func, resolver) {
if (!isFunction(func)) {
throw new TypeError;
}
var memoized = function() {
var cache = memoized.cache,
key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
return hasOwnProperty.call(cache, key)
? cache[key]
: (cache[key] = func.apply(this, arguments));
}
memoized.cache = {};
return memoized;
}
/**
* Creates a function that is restricted to execute `func` once. Repeat calls to
* the function will return the value of the first call. The `func` is executed
* with the `this` binding of the created function.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to restrict.
* @returns {Function} Returns the new restricted function.
* @example
*
* var initialize = _.once(createApplication);
* initialize();
* initialize();
* // `initialize` executes `createApplication` once
*/
function once(func) {
var ran,
result;
if (!isFunction(func)) {
throw new TypeError;
}
return function() {
if (ran) {
return result;
}
ran = true;
result = func.apply(this, arguments);
// clear the `func` variable so the function may be garbage collected
func = null;
return result;
};
}
/**
* Creates a function that, when called, invokes `func` with any additional
* `partial` arguments prepended to those provided to the new function. This
* method is similar to `_.bind` except it does **not** alter the `this` binding.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to partially apply arguments to.
* @param {...*} [arg] Arguments to be partially applied.
* @returns {Function} Returns the new partially applied function.
* @example
*
* var greet = function(greeting, name) { return greeting + ' ' + name; };
* var hi = _.partial(greet, 'hi');
* hi('fred');
* // => 'hi fred'
*/
function partial(func) {
return createWrapper(func, 16, slice(arguments, 1));
}
/**
* This method is like `_.partial` except that `partial` arguments are
* appended to those provided to the new function.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to partially apply arguments to.
* @param {...*} [arg] Arguments to be partially applied.
* @returns {Function} Returns the new partially applied function.
* @example
*
* var defaultsDeep = _.partialRight(_.merge, _.defaults);
*
* var options = {
* 'variable': 'data',
* 'imports': { 'jq': $ }
* };
*
* defaultsDeep(options, _.templateSettings);
*
* options.variable
* // => 'data'
*
* options.imports
* // => { '_': _, 'jq': $ }
*/
function partialRight(func) {
return createWrapper(func, 32, null, slice(arguments, 1));
}
/**
* Creates a function that, when executed, will only call the `func` function
* at most once per every `wait` milliseconds. Provide an options object to
* indicate that `func` should be invoked on the leading and/or trailing edge
* of the `wait` timeout. Subsequent calls to the throttled function will
* return the result of the last `func` call.
*
* Note: If `leading` and `trailing` options are `true` `func` will be called
* on the trailing edge of the timeout only if the the throttled function is
* invoked more than once during the `wait` timeout.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to throttle.
* @param {number} wait The number of milliseconds to throttle executions to.
* @param {Object} [options] The options object.
* @param {boolean} [options.leading=true] Specify execution on the leading edge of the timeout.
* @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
* @returns {Function} Returns the new throttled function.
* @example
*
* // avoid excessively updating the position while scrolling
* var throttled = _.throttle(updatePosition, 100);
* jQuery(window).on('scroll', throttled);
*
* // execute `renewToken` when the click event is fired, but not more than once every 5 minutes
* jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
* 'trailing': false
* }));
*/
function throttle(func, wait, options) {
var leading = true,
trailing = true;
if (!isFunction(func)) {
throw new TypeError;
}
if (options === false) {
leading = false;
} else if (isObject(options)) {
leading = 'leading' in options ? options.leading : leading;
trailing = 'trailing' in options ? options.trailing : trailing;
}
debounceOptions.leading = leading;
debounceOptions.maxWait = wait;
debounceOptions.trailing = trailing;
return debounce(func, wait, debounceOptions);
}
/**
* Creates a function that provides `value` to the wrapper function as its
* first argument. Additional arguments provided to the function are appended
* to those provided to the wrapper function. The wrapper is executed with
* the `this` binding of the created function.
*
* @static
* @memberOf _
* @category Functions
* @param {*} value The value to wrap.
* @param {Function} wrapper The wrapper function.
* @returns {Function} Returns the new function.
* @example
*
* var p = _.wrap(_.escape, function(func, text) {
* return '<p>' + func(text) + '</p>';
* });
*
* p('Fred, Wilma, & Pebbles');
* // => '<p>Fred, Wilma, & Pebbles</p>'
*/
function wrap(value, wrapper) {
return createWrapper(wrapper, 16, [value]);
}
/*--------------------------------------------------------------------------*/
/**
* Creates a function that returns `value`.
*
* @static
* @memberOf _
* @category Utilities
* @param {*} value The value to return from the new function.
* @returns {Function} Returns the new function.
* @example
*
* var object = { 'name': 'fred' };
* var getter = _.constant(object);
* getter() === object;
* // => true
*/
function constant(value) {
return function() {
return value;
};
}
/**
* Produces a callback bound to an optional `thisArg`. If `func` is a property
* name the created callback will return the property value for a given element.
* If `func` is an object the created callback will return `true` for elements
* that contain the equivalent object properties, otherwise it will return `false`.
*
* @static
* @memberOf _
* @category Utilities
* @param {*} [func=identity] The value to convert to a callback.
* @param {*} [thisArg] The `this` binding of the created callback.
* @param {number} [argCount] The number of arguments the callback accepts.
* @returns {Function} Returns a callback function.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* // wrap to create custom callback shorthands
* _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
* var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
* return !match ? func(callback, thisArg) : function(object) {
* return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
* };
* });
*
* _.filter(characters, 'age__gt38');
* // => [{ 'name': 'fred', 'age': 40 }]
*/
function createCallback(func, thisArg, argCount) {
var type = typeof func;
if (func == null || type == 'function') {
return baseCreateCallback(func, thisArg, argCount);
}
// handle "_.pluck" style callback shorthands
if (type != 'object') {
return property(func);
}
var props = keys(func),
key = props[0],
a = func[key];
// handle "_.where" style callback shorthands
if (props.length == 1 && a === a && !isObject(a)) {
// fast path the common case of providing an object with a single
// property containing a primitive value
return function(object) {
var b = object[key];
return a === b && (a !== 0 || (1 / a == 1 / b));
};
}
return function(object) {
var length = props.length,
result = false;
while (length--) {
if (!(result = baseIsEqual(object[props[length]], func[props[length]], null, true))) {
break;
}
}
return result;
};
}
/**
* Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
* corresponding HTML entities.
*
* @static
* @memberOf _
* @category Utilities
* @param {string} string The string to escape.
* @returns {string} Returns the escaped string.
* @example
*
* _.escape('Fred, Wilma, & Pebbles');
* // => 'Fred, Wilma, & Pebbles'
*/
function escape(string) {
return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar);
}
/**
* This method returns the first argument provided to it.
*
* @static
* @memberOf _
* @category Utilities
* @param {*} value Any value.
* @returns {*} Returns `value`.
* @example
*
* var object = { 'name': 'fred' };
* _.identity(object) === object;
* // => true
*/
function identity(value) {
return value;
}
/**
* Adds function properties of a source object to the destination object.
* If `object` is a function methods will be added to its prototype as well.
*
* @static
* @memberOf _
* @category Utilities
* @param {Function|Object} [object=lodash] object The destination object.
* @param {Object} source The object of functions to add.
* @param {Object} [options] The options object.
* @param {boolean} [options.chain=true] Specify whether the functions added are chainable.
* @example
*
* function capitalize(string) {
* return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
* }
*
* _.mixin({ 'capitalize': capitalize });
* _.capitalize('fred');
* // => 'Fred'
*
* _('fred').capitalize().value();
* // => 'Fred'
*
* _.mixin({ 'capitalize': capitalize }, { 'chain': false });
* _('fred').capitalize();
* // => 'Fred'
*/
function mixin(object, source, options) {
var chain = true,
methodNames = source && functions(source);
Eif (!source || (!options && !methodNames.length)) {
Eif (options == null) {
options = source;
}
ctor = lodashWrapper;
source = object;
object = lodash;
methodNames = functions(source);
}
if (options === false) {
chain = false;
} else Iif (isObject(options) && 'chain' in options) {
chain = options.chain;
}
var ctor = object,
isFunc = isFunction(ctor);
forEach(methodNames, function(methodName) {
var func = object[methodName] = source[methodName];
Eif (isFunc) {
ctor.prototype[methodName] = function() {
var chainAll = this.__chain__,
value = this.__wrapped__,
args = [value];
push.apply(args, arguments);
var result = func.apply(object, args);
if (chain || chainAll) {
if (value === result && isObject(result)) {
return this;
}
result = new ctor(result);
result.__chain__ = chainAll;
}
return result;
};
}
});
}
/**
* Reverts the '_' variable to its previous value and returns a reference to
* the `lodash` function.
*
* @static
* @memberOf _
* @category Utilities
* @returns {Function} Returns the `lodash` function.
* @example
*
* var lodash = _.noConflict();
*/
function noConflict() {
context._ = oldDash;
return this;
}
/**
* A no-operation function.
*
* @static
* @memberOf _
* @category Utilities
* @example
*
* var object = { 'name': 'fred' };
* _.noop(object) === undefined;
* // => true
*/
function noop() {
// no operation performed
}
/**
* Gets the number of milliseconds that have elapsed since the Unix epoch
* (1 January 1970 00:00:00 UTC).
*
* @static
* @memberOf _
* @category Utilities
* @example
*
* var stamp = _.now();
* _.defer(function() { console.log(_.now() - stamp); });
* // => logs the number of milliseconds it took for the deferred function to be called
*/
var now = isNative(now = Date.now) && now || function() {
return new Date().getTime();
};
/**
* Converts the given value into an integer of the specified radix.
* If `radix` is `undefined` or `0` a `radix` of `10` is used unless the
* `value` is a hexadecimal, in which case a `radix` of `16` is used.
*
* Note: This method avoids differences in native ES3 and ES5 `parseInt`
* implementations. See http://es5.github.io/#E.
*
* @static
* @memberOf _
* @category Utilities
* @param {string} value The value to parse.
* @param {number} [radix] The radix used to interpret the value to parse.
* @returns {number} Returns the new integer value.
* @example
*
* _.parseInt('08');
* // => 8
*/
var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) {
// Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt`
return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0);
};
/**
* Creates a "_.pluck" style function, which returns the `key` value of a
* given object.
*
* @static
* @memberOf _
* @category Utilities
* @param {string} key The name of the property to retrieve.
* @returns {Function} Returns the new function.
* @example
*
* var characters = [
* { 'name': 'fred', 'age': 40 },
* { 'name': 'barney', 'age': 36 }
* ];
*
* var getName = _.property('name');
*
* _.map(characters, getName);
* // => ['barney', 'fred']
*
* _.sortBy(characters, getName);
* // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
*/
function property(key) {
return function(object) {
return object[key];
};
}
/**
* Produces a random number between `min` and `max` (inclusive). If only one
* argument is provided a number between `0` and the given number will be
* returned. If `floating` is truey or either `min` or `max` are floats a
* floating-point number will be returned instead of an integer.
*
* @static
* @memberOf _
* @category Utilities
* @param {number} [min=0] The minimum possible value.
* @param {number} [max=1] The maximum possible value.
* @param {boolean} [floating=false] Specify returning a floating-point number.
* @returns {number} Returns a random number.
* @example
*
* _.random(0, 5);
* // => an integer between 0 and 5
*
* _.random(5);
* // => also an integer between 0 and 5
*
* _.random(5, true);
* // => a floating-point number between 0 and 5
*
* _.random(1.2, 5.2);
* // => a floating-point number between 1.2 and 5.2
*/
function random(min, max, floating) {
var noMin = min == null,
noMax = max == null;
if (floating == null) {
if (typeof min == 'boolean' && noMax) {
floating = min;
min = 1;
}
else if (!noMax && typeof max == 'boolean') {
floating = max;
noMax = true;
}
}
if (noMin && noMax) {
max = 1;
}
min = +min || 0;
if (noMax) {
max = min;
min = 0;
} else {
max = +max || 0;
}
if (floating || min % 1 || max % 1) {
var rand = nativeRandom();
return nativeMin(min + (rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1)))), max);
}
return baseRandom(min, max);
}
/**
* Resolves the value of property `key` on `object`. If `key` is a function
* it will be invoked with the `this` binding of `object` and its result returned,
* else the property value is returned. If `object` is falsey then `undefined`
* is returned.
*
* @static
* @memberOf _
* @category Utilities
* @param {Object} object The object to inspect.
* @param {string} key The name of the property to resolve.
* @returns {*} Returns the resolved value.
* @example
*
* var object = {
* 'cheese': 'crumpets',
* 'stuff': function() {
* return 'nonsense';
* }
* };
*
* _.result(object, 'cheese');
* // => 'crumpets'
*
* _.result(object, 'stuff');
* // => 'nonsense'
*/
function result(object, key) {
if (object) {
var value = object[key];
return isFunction(value) ? object[key]() : value;
}
}
/**
* A micro-templating method that handles arbitrary delimiters, preserves
* whitespace, and correctly escapes quotes within interpolated code.
*
* Note: In the development build, `_.template` utilizes sourceURLs for easier
* debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
*
* For more information on precompiling templates see:
* https://lodash.com/custom-builds
*
* For more information on Chrome extension sandboxes see:
* http://developer.chrome.com/stable/extensions/sandboxingEval.html
*
* @static
* @memberOf _
* @category Utilities
* @param {string} text The template text.
* @param {Object} data The data object used to populate the text.
* @param {Object} [options] The options object.
* @param {RegExp} [options.escape] The "escape" delimiter.
* @param {RegExp} [options.evaluate] The "evaluate" delimiter.
* @param {Object} [options.imports] An object to import into the template as local variables.
* @param {RegExp} [options.interpolate] The "interpolate" delimiter.
* @param {string} [sourceURL] The sourceURL of the template's compiled source.
* @param {string} [variable] The data object variable name.
* @returns {Function|string} Returns a compiled function when no `data` object
* is given, else it returns the interpolated text.
* @example
*
* // using the "interpolate" delimiter to create a compiled template
* var compiled = _.template('hello <%= name %>');
* compiled({ 'name': 'fred' });
* // => 'hello fred'
*
* // using the "escape" delimiter to escape HTML in data property values
* _.template('<b><%- value %></b>', { 'value': '<script>' });
* // => '<b><script></b>'
*
* // using the "evaluate" delimiter to generate HTML
* var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
* _.template(list, { 'people': ['fred', 'barney'] });
* // => '<li>fred</li><li>barney</li>'
*
* // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
* _.template('hello ${ name }', { 'name': 'pebbles' });
* // => 'hello pebbles'
*
* // using the internal `print` function in "evaluate" delimiters
* _.template('<% print("hello " + name); %>!', { 'name': 'barney' });
* // => 'hello barney!'
*
* // using a custom template delimiters
* _.templateSettings = {
* 'interpolate': /{{([\s\S]+?)}}/g
* };
*
* _.template('hello {{ name }}!', { 'name': 'mustache' });
* // => 'hello mustache!'
*
* // using the `imports` option to import jQuery
* var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>';
* _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } });
* // => '<li>fred</li><li>barney</li>'
*
* // using the `sourceURL` option to specify a custom sourceURL for the template
* var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
* compiled(data);
* // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
*
* // using the `variable` option to ensure a with-statement isn't used in the compiled template
* var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
* compiled.source;
* // => function(data) {
* var __t, __p = '', __e = _.escape;
* __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
* return __p;
* }
*
* // using the `source` property to inline compiled templates for meaningful
* // line numbers in error messages and a stack trace
* fs.writeFileSync(path.join(cwd, 'jst.js'), '\
* var JST = {\
* "main": ' + _.template(mainText).source + '\
* };\
* ');
*/
function template(text, data, options) {
// based on John Resig's `tmpl` implementation
// http://ejohn.org/blog/javascript-micro-templating/
// and Laura Doktorova's doT.js
// https://github.com/olado/doT
var settings = lodash.templateSettings;
text = String(text || '');
// avoid missing dependencies when `iteratorTemplate` is not defined
options = defaults({}, options, settings);
var imports = defaults({}, options.imports, settings.imports),
importsKeys = keys(imports),
importsValues = values(imports);
var isEvaluating,
index = 0,
interpolate = options.interpolate || reNoMatch,
source = "__p += '";
// compile the regexp to match each delimiter
var reDelimiters = RegExp(
(options.escape || reNoMatch).source + '|' +
interpolate.source + '|' +
(interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
(options.evaluate || reNoMatch).source + '|$'
, 'g');
text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
interpolateValue || (interpolateValue = esTemplateValue);
// escape characters that cannot be included in string literals
source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);
// replace delimiters with snippets
if (escapeValue) {
source += "' +\n__e(" + escapeValue + ") +\n'";
}
if (evaluateValue) {
isEvaluating = true;
source += "';\n" + evaluateValue + ";\n__p += '";
}
if (interpolateValue) {
source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
}
index = offset + match.length;
// the JS engine embedded in Adobe products requires returning the `match`
// string in order to produce the correct `offset` value
return match;
});
source += "';\n";
// if `variable` is not specified, wrap a with-statement around the generated
// code to add the data object to the top of the scope chain
var variable = options.variable,
hasVariable = variable;
if (!hasVariable) {
variable = 'obj';
source = 'with (' + variable + ') {\n' + source + '\n}\n';
}
// cleanup code by stripping empty strings
source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
.replace(reEmptyStringMiddle, '$1')
.replace(reEmptyStringTrailing, '$1;');
// frame code as the function body
source = 'function(' + variable + ') {\n' +
(hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
"var __t, __p = '', __e = _.escape" +
(isEvaluating
? ', __j = Array.prototype.join;\n' +
"function print() { __p += __j.call(arguments, '') }\n"
: ';\n'
) +
source +
'return __p\n}';
// Use a sourceURL for easier debugging.
// http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
var sourceURL = '\n/*\n//# sourceURL=' + (options.sourceURL || '/lodash/template/source[' + (templateCounter++) + ']') + '\n*/';
try {
var result = Function(importsKeys, 'return ' + source + sourceURL).apply(undefined, importsValues);
} catch(e) {
e.source = source;
throw e;
}
if (data) {
return result(data);
}
// provide the compiled function's source by its `toString` method, in
// supported environments, or the `source` property as a convenience for
// inlining compiled templates during the build process
result.source = source;
return result;
}
/**
* Executes the callback `n` times, returning an array of the results
* of each callback execution. The callback is bound to `thisArg` and invoked
* with one argument; (index).
*
* @static
* @memberOf _
* @category Utilities
* @param {number} n The number of times to execute the callback.
* @param {Function} callback The function called per iteration.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns an array of the results of each `callback` execution.
* @example
*
* var diceRolls = _.times(3, _.partial(_.random, 1, 6));
* // => [3, 6, 4]
*
* _.times(3, function(n) { mage.castSpell(n); });
* // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
*
* _.times(3, function(n) { this.cast(n); }, mage);
* // => also calls `mage.castSpell(n)` three times
*/
function times(n, callback, thisArg) {
n = (n = +n) > -1 ? n : 0;
var index = -1,
result = Array(n);
callback = baseCreateCallback(callback, thisArg, 1);
while (++index < n) {
result[index] = callback(index);
}
return result;
}
/**
* The inverse of `_.escape` this method converts the HTML entities
* `&`, `<`, `>`, `"`, and `'` in `string` to their
* corresponding characters.
*
* @static
* @memberOf _
* @category Utilities
* @param {string} string The string to unescape.
* @returns {string} Returns the unescaped string.
* @example
*
* _.unescape('Fred, Barney & Pebbles');
* // => 'Fred, Barney & Pebbles'
*/
function unescape(string) {
return string == null ? '' : String(string).replace(reEscapedHtml, unescapeHtmlChar);
}
/**
* Generates a unique ID. If `prefix` is provided the ID will be appended to it.
*
* @static
* @memberOf _
* @category Utilities
* @param {string} [prefix] The value to prefix the ID with.
* @returns {string} Returns the unique ID.
* @example
*
* _.uniqueId('contact_');
* // => 'contact_104'
*
* _.uniqueId();
* // => '105'
*/
function uniqueId(prefix) {
var id = ++idCounter;
return String(prefix == null ? '' : prefix) + id;
}
/*--------------------------------------------------------------------------*/
/**
* Creates a `lodash` object that wraps the given value with explicit
* method chaining enabled.
*
* @static
* @memberOf _
* @category Chaining
* @param {*} value The value to wrap.
* @returns {Object} Returns the wrapper object.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 },
* { 'name': 'pebbles', 'age': 1 }
* ];
*
* var youngest = _.chain(characters)
* .sortBy('age')
* .map(function(chr) { return chr.name + ' is ' + chr.age; })
* .first()
* .value();
* // => 'pebbles is 1'
*/
function chain(value) {
value = new lodashWrapper(value);
value.__chain__ = true;
return value;
}
/**
* Invokes `interceptor` with the `value` as the first argument and then
* returns `value`. The purpose of this method is to "tap into" a method
* chain in order to perform operations on intermediate results within
* the chain.
*
* @static
* @memberOf _
* @category Chaining
* @param {*} value The value to provide to `interceptor`.
* @param {Function} interceptor The function to invoke.
* @returns {*} Returns `value`.
* @example
*
* _([1, 2, 3, 4])
* .tap(function(array) { array.pop(); })
* .reverse()
* .value();
* // => [3, 2, 1]
*/
function tap(value, interceptor) {
interceptor(value);
return value;
}
/**
* Enables explicit method chaining on the wrapper object.
*
* @name chain
* @memberOf _
* @category Chaining
* @returns {*} Returns the wrapper object.
* @example
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40 }
* ];
*
* // without explicit chaining
* _(characters).first();
* // => { 'name': 'barney', 'age': 36 }
*
* // with explicit chaining
* _(characters).chain()
* .first()
* .pick('age')
* .value();
* // => { 'age': 36 }
*/
function wrapperChain() {
this.__chain__ = true;
return this;
}
/**
* Produces the `toString` result of the wrapped value.
*
* @name toString
* @memberOf _
* @category Chaining
* @returns {string} Returns the string result.
* @example
*
* _([1, 2, 3]).toString();
* // => '1,2,3'
*/
function wrapperToString() {
return String(this.__wrapped__);
}
/**
* Extracts the wrapped value.
*
* @name valueOf
* @memberOf _
* @alias value
* @category Chaining
* @returns {*} Returns the wrapped value.
* @example
*
* _([1, 2, 3]).valueOf();
* // => [1, 2, 3]
*/
function wrapperValueOf() {
return this.__wrapped__;
}
/*--------------------------------------------------------------------------*/
// add functions that return wrapped values when chaining
lodash.after = after;
lodash.assign = assign;
lodash.at = at;
lodash.bind = bind;
lodash.bindAll = bindAll;
lodash.bindKey = bindKey;
lodash.chain = chain;
lodash.compact = compact;
lodash.compose = compose;
lodash.constant = constant;
lodash.countBy = countBy;
lodash.create = create;
lodash.createCallback = createCallback;
lodash.curry = curry;
lodash.debounce = debounce;
lodash.defaults = defaults;
lodash.defer = defer;
lodash.delay = delay;
lodash.difference = difference;
lodash.filter = filter;
lodash.flatten = flatten;
lodash.forEach = forEach;
lodash.forEachRight = forEachRight;
lodash.forIn = forIn;
lodash.forInRight = forInRight;
lodash.forOwn = forOwn;
lodash.forOwnRight = forOwnRight;
lodash.functions = functions;
lodash.groupBy = groupBy;
lodash.indexBy = indexBy;
lodash.initial = initial;
lodash.intersection = intersection;
lodash.invert = invert;
lodash.invoke = invoke;
lodash.keys = keys;
lodash.map = map;
lodash.mapValues = mapValues;
lodash.max = max;
lodash.memoize = memoize;
lodash.merge = merge;
lodash.min = min;
lodash.omit = omit;
lodash.once = once;
lodash.pairs = pairs;
lodash.partial = partial;
lodash.partialRight = partialRight;
lodash.pick = pick;
lodash.pluck = pluck;
lodash.property = property;
lodash.pull = pull;
lodash.range = range;
lodash.reject = reject;
lodash.remove = remove;
lodash.rest = rest;
lodash.shuffle = shuffle;
lodash.sortBy = sortBy;
lodash.tap = tap;
lodash.throttle = throttle;
lodash.times = times;
lodash.toArray = toArray;
lodash.transform = transform;
lodash.union = union;
lodash.uniq = uniq;
lodash.values = values;
lodash.where = where;
lodash.without = without;
lodash.wrap = wrap;
lodash.xor = xor;
lodash.zip = zip;
lodash.zipObject = zipObject;
// add aliases
lodash.collect = map;
lodash.drop = rest;
lodash.each = forEach;
lodash.eachRight = forEachRight;
lodash.extend = assign;
lodash.methods = functions;
lodash.object = zipObject;
lodash.select = filter;
lodash.tail = rest;
lodash.unique = uniq;
lodash.unzip = zip;
// add functions to `lodash.prototype`
mixin(lodash);
/*--------------------------------------------------------------------------*/
// add functions that return unwrapped values when chaining
lodash.clone = clone;
lodash.cloneDeep = cloneDeep;
lodash.contains = contains;
lodash.escape = escape;
lodash.every = every;
lodash.find = find;
lodash.findIndex = findIndex;
lodash.findKey = findKey;
lodash.findLast = findLast;
lodash.findLastIndex = findLastIndex;
lodash.findLastKey = findLastKey;
lodash.has = has;
lodash.identity = identity;
lodash.indexOf = indexOf;
lodash.isArguments = isArguments;
lodash.isArray = isArray;
lodash.isBoolean = isBoolean;
lodash.isDate = isDate;
lodash.isElement = isElement;
lodash.isEmpty = isEmpty;
lodash.isEqual = isEqual;
lodash.isFinite = isFinite;
lodash.isFunction = isFunction;
lodash.isNaN = isNaN;
lodash.isNull = isNull;
lodash.isNumber = isNumber;
lodash.isObject = isObject;
lodash.isPlainObject = isPlainObject;
lodash.isRegExp = isRegExp;
lodash.isString = isString;
lodash.isUndefined = isUndefined;
lodash.lastIndexOf = lastIndexOf;
lodash.mixin = mixin;
lodash.noConflict = noConflict;
lodash.noop = noop;
lodash.now = now;
lodash.parseInt = parseInt;
lodash.random = random;
lodash.reduce = reduce;
lodash.reduceRight = reduceRight;
lodash.result = result;
lodash.runInContext = runInContext;
lodash.size = size;
lodash.some = some;
lodash.sortedIndex = sortedIndex;
lodash.template = template;
lodash.unescape = unescape;
lodash.uniqueId = uniqueId;
// add aliases
lodash.all = every;
lodash.any = some;
lodash.detect = find;
lodash.findWhere = find;
lodash.foldl = reduce;
lodash.foldr = reduceRight;
lodash.include = contains;
lodash.inject = reduce;
mixin(function() {
var source = {}
forOwn(lodash, function(func, methodName) {
if (!lodash.prototype[methodName]) {
source[methodName] = func;
}
});
return source;
}(), false);
/*--------------------------------------------------------------------------*/
// add functions capable of returning wrapped and unwrapped values when chaining
lodash.first = first;
lodash.last = last;
lodash.sample = sample;
// add aliases
lodash.take = first;
lodash.head = first;
forOwn(lodash, function(func, methodName) {
var callbackable = methodName !== 'sample';
if (!lodash.prototype[methodName]) {
lodash.prototype[methodName]= function(n, guard) {
var chainAll = this.__chain__,
result = func(this.__wrapped__, n, guard);
return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function')))
? result
: new lodashWrapper(result, chainAll);
};
}
});
/*--------------------------------------------------------------------------*/
/**
* The semantic version number.
*
* @static
* @memberOf _
* @type string
*/
lodash.VERSION = '2.4.2';
// add "Chaining" functions to the wrapper
lodash.prototype.chain = wrapperChain;
lodash.prototype.toString = wrapperToString;
lodash.prototype.value = wrapperValueOf;
lodash.prototype.valueOf = wrapperValueOf;
// add `Array` functions that return unwrapped values
forEach(['join', 'pop', 'shift'], function(methodName) {
var func = arrayRef[methodName];
lodash.prototype[methodName] = function() {
var chainAll = this.__chain__,
result = func.apply(this.__wrapped__, arguments);
return chainAll
? new lodashWrapper(result, chainAll)
: result;
};
});
// add `Array` functions that return the existing wrapped value
forEach(['push', 'reverse', 'sort', 'unshift'], function(methodName) {
var func = arrayRef[methodName];
lodash.prototype[methodName] = function() {
func.apply(this.__wrapped__, arguments);
return this;
};
});
// add `Array` functions that return new wrapped values
forEach(['concat', 'slice', 'splice'], function(methodName) {
var func = arrayRef[methodName];
lodash.prototype[methodName] = function() {
return new lodashWrapper(func.apply(this.__wrapped__, arguments), this.__chain__);
};
});
return lodash;
}
/*--------------------------------------------------------------------------*/
// expose Lo-Dash
var _ = runInContext();
// some AMD build optimizers like r.js check for condition patterns like the following:
Iif (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// Expose Lo-Dash to the global object even when an AMD loader is present in
// case Lo-Dash is loaded with a RequireJS shim config.
// See http://requirejs.org/docs/api.html#config-shim
root._ = _;
// define as an anonymous module so, through path mapping, it can be
// referenced as the "underscore" module
define(function() {
return _;
});
}
// check for `exports` after `define` in case a build optimizer adds an `exports` object
else Eif (freeExports && freeModule) {
// in Node.js or RingoJS
Eif (moduleExports) {
(freeModule.exports = _)._ = _;
}
// in Narwhal or Rhino -require
else {
freeExports._ = _;
}
}
else {
// in a browser or Rhino
root._ = _;
}
}.call(this));
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| fetch_plugins.js | 54.55% | (6 / 11) | 0% | (0 / 2) | 25% | (1 / 4) | 60% | (6 / 10) | |
| get.js | 66.67% | (4 / 6) | 100% | (0 / 0) | 0% | (0 / 1) | 66.67% | (4 / 6) | |
| index.js | 100% | (2 / 2) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (2 / 2) | |
| root.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 1 1 1 1 1 1 | var Promise = require('bluebird')
var get = require('./get')
var yaml = require('js-yaml')
module.exports = function(root) {
var url = root+'/plugins.yml'
return function() {
return new Promise(function(resolve, reject){
get(url, function(err, res, body) {
if (err) reject(err);
resolve(yaml.load(body));
});
})
}
}
|
| 1 2 3 4 5 6 7 8 9 | 1 1 1 1 | var root = require('./root')
var needle = require('needle')
module.exports = get;
function get(url, cb) {
console.log("GET "+url.replace(root, '[ecosystem]'))
needle.get(url, cb)
}
|
| 1 2 3 4 5 6 | 1 1 | var root = require('./root')
module.exports = {
fetchPlugins: require('./fetch_plugins')(root),
}
|
| 1 2 3 4 5 6 7 | 1 | /* When editing ecosystem or client you can clone it and run * python -m SimpleHTTPServer * and use this export instead: */ //module.exports = 'http://localhost:8000' module.exports = 'https://raw.githubusercontent.com/Strider-CD/ecosystem-index/master' |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| any.js | 38.46% | (5 / 13) | 100% | (0 / 0) | 25% | (1 / 4) | 38.46% | (5 / 13) | |
| async.js | 35.11% | (33 / 94) | 8.33% | (2 / 24) | 3.85% | (1 / 26) | 34.44% | (31 / 90) | |
| bind.js | 22.5% | (9 / 40) | 0% | (0 / 10) | 10% | (1 / 10) | 23.08% | (9 / 39) | |
| bluebird.js | 63.64% | (7 / 11) | 25% | (1 / 4) | 0% | (0 / 1) | 75% | (6 / 8) | |
| call_get.js | 30.67% | (23 / 75) | 7.14% | (2 / 28) | 8.33% | (1 / 12) | 33.33% | (23 / 69) | |
| cancel.js | 25% | (9 / 36) | 0% | (0 / 10) | 16.67% | (1 / 6) | 28.13% | (9 / 32) | |
| captured_trace.js | 23.72% | (74 / 312) | 13.61% | (23 / 169) | 16.67% | (6 / 36) | 24.58% | (74 / 301) | |
| catch_filter.js | 22.92% | (11 / 48) | 0% | (0 / 23) | 25% | (1 / 4) | 23.4% | (11 / 47) | |
| context.js | 42.31% | (11 / 26) | 0% | (0 / 12) | 16.67% | (1 / 6) | 47.83% | (11 / 23) | |
| debuggability.js | 35.48% | (33 / 93) | 16.67% | (9 / 54) | 4.35% | (1 / 23) | 36.67% | (33 / 90) | |
| direct_resolve.js | 32.26% | (10 / 31) | 0% | (0 / 14) | 10% | (1 / 10) | 34.48% | (10 / 29) | |
| each.js | 66.67% | (4 / 6) | 100% | (0 / 0) | 33.33% | (1 / 3) | 66.67% | (4 / 6) | |
| errors.js | 46.15% | (30 / 65) | 11.11% | (2 / 18) | 25% | (1 / 4) | 46.88% | (30 / 64) | |
| es5.js | 12.5% | (4 / 32) | 14.29% | (1 / 7) | 11.11% | (1 / 9) | 12.5% | (4 / 32) | |
| filter.js | 66.67% | (4 / 6) | 100% | (0 / 0) | 33.33% | (1 / 3) | 66.67% | (4 / 6) | |
| finally.js | 26.92% | (14 / 52) | 0% | (0 / 26) | 7.69% | (1 / 13) | 27.45% | (14 / 51) | |
| generators.js | 20.73% | (17 / 82) | 0% | (0 / 20) | 8.33% | (1 / 12) | 21.25% | (17 / 80) | |
| join.js | 35.21% | (25 / 71) | 7.14% | (2 / 28) | 42.86% | (3 / 7) | 36.36% | (24 / 66) | |
| map.js | 19.79% | (19 / 96) | 0% | (0 / 51) | 9.09% | (1 / 11) | 21.84% | (19 / 87) | |
| method.js | 21.43% | (6 / 28) | 0% | (0 / 8) | 20% | (1 / 5) | 21.43% | (6 / 28) | |
| nodeify.js | 25.71% | (9 / 35) | 0% | (0 / 18) | 20% | (1 / 5) | 26.47% | (9 / 34) | |
| progress.js | 23.26% | (10 / 43) | 0% | (0 / 24) | 16.67% | (1 / 6) | 23.81% | (10 / 42) | |
| promise.js | 33.26% | (155 / 466) | 1.87% | (4 / 214) | 3.9% | (3 / 77) | 34.52% | (155 / 449) | |
| promise_array.js | 19.77% | (17 / 86) | 0% | (0 / 32) | 7.14% | (1 / 14) | 19.77% | (17 / 86) | |
| promise_resolver.js | 35.9% | (28 / 78) | 9.09% | (2 / 22) | 0% | (0 / 15) | 37.84% | (28 / 74) | |
| promisify.js | 23.33% | (35 / 150) | 2.99% | (2 / 67) | 4.35% | (1 / 23) | 24.14% | (35 / 145) | |
| props.js | 30.43% | (14 / 46) | 0% | (0 / 8) | 10% | (1 / 10) | 30.43% | (14 / 46) | |
| queue.js | 24.59% | (15 / 61) | 0% | (0 / 4) | 9.09% | (1 / 11) | 24.59% | (15 / 61) | |
| race.js | 23.08% | (6 / 26) | 0% | (0 / 10) | 16.67% | (1 / 6) | 23.08% | (6 / 26) | |
| reduce.js | 14.02% | (15 / 107) | 0% | (0 / 56) | 11.11% | (1 / 9) | 14.85% | (15 / 101) | |
| schedule.js | 30.77% | (8 / 26) | 23.53% | (4 / 17) | 0% | (0 / 7) | 32% | (8 / 25) | |
| settle.js | 40% | (10 / 25) | 0% | (0 / 2) | 14.29% | (1 / 7) | 40% | (10 / 25) | |
| some.js | 34.72% | (25 / 72) | 0% | (0 / 21) | 5.26% | (1 / 19) | 34.72% | (25 / 72) | |
| synchronous_inspection.js | 34.69% | (17 / 49) | 0% | (0 / 10) | 6.25% | (1 / 16) | 34.69% | (17 / 49) | |
| thenables.js | 22.03% | (13 / 59) | 0% | (0 / 30) | 12.5% | (1 / 8) | 25% | (13 / 52) | |
| timers.js | 20.83% | (10 / 48) | 0% | (0 / 14) | 10% | (1 / 10) | 23.26% | (10 / 43) | |
| using.js | 20.45% | (27 / 132) | 0% | (0 / 45) | 4% | (1 / 25) | 21.43% | (27 / 126) | |
| util.js | 41.71% | (73 / 175) | 22.89% | (19 / 83) | 37.5% | (12 / 32) | 42.59% | (69 / 162) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1 1 1 1 1 | "use strict";
module.exports = function(Promise) {
var SomePromiseArray = Promise._SomePromiseArray;
function any(promises) {
var ret = new SomePromiseArray(promises);
var promise = ret.promise();
ret.setHowMany(1);
ret.setUnwrap();
ret.init();
return promise;
}
Promise.any = function (promises) {
return any(promises);
};
Promise.prototype.any = function () {
return any(this);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
var firstLineError;
try {throw new Error(); } catch (e) {firstLineError = e;}
var schedule = require("./schedule.js");
var Queue = require("./queue.js");
var util = require("./util.js");
function Async() {
this._isTickUsed = false;
this._lateQueue = new Queue(16);
this._normalQueue = new Queue(16);
this._trampolineEnabled = true;
var self = this;
this.drainQueues = function () {
self._drainQueues();
};
this._schedule =
schedule.isStatic ? schedule(this.drainQueues) : schedule;
}
Async.prototype.disableTrampolineIfNecessary = function() {
if (util.hasDevTools) {
this._trampolineEnabled = false;
}
};
Async.prototype.enableTrampoline = function() {
if (!this._trampolineEnabled) {
this._trampolineEnabled = true;
this._schedule = function(fn) {
setTimeout(fn, 0);
};
}
};
Async.prototype.haveItemsQueued = function () {
return this._normalQueue.length() > 0;
};
Async.prototype.throwLater = function(fn, arg) {
if (arguments.length === 1) {
arg = fn;
fn = function () { throw arg; };
}
if (typeof setTimeout !== "undefined") {
setTimeout(function() {
fn(arg);
}, 0);
} else try {
this._schedule(function() {
fn(arg);
});
} catch (e) {
throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");
}
};
function AsyncInvokeLater(fn, receiver, arg) {
this._lateQueue.push(fn, receiver, arg);
this._queueTick();
}
function AsyncInvoke(fn, receiver, arg) {
this._normalQueue.push(fn, receiver, arg);
this._queueTick();
}
function AsyncSettlePromises(promise) {
this._normalQueue._pushOne(promise);
this._queueTick();
}
Eif (!util.hasDevTools) {
Async.prototype.invokeLater = AsyncInvokeLater;
Async.prototype.invoke = AsyncInvoke;
Async.prototype.settlePromises = AsyncSettlePromises;
} else {
if (schedule.isStatic) {
schedule = function(fn) { setTimeout(fn, 0); };
}
Async.prototype.invokeLater = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvokeLater.call(this, fn, receiver, arg);
} else {
this._schedule(function() {
setTimeout(function() {
fn.call(receiver, arg);
}, 100);
});
}
};
Async.prototype.invoke = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvoke.call(this, fn, receiver, arg);
} else {
this._schedule(function() {
fn.call(receiver, arg);
});
}
};
Async.prototype.settlePromises = function(promise) {
if (this._trampolineEnabled) {
AsyncSettlePromises.call(this, promise);
} else {
this._schedule(function() {
promise._settlePromises();
});
}
};
}
Async.prototype.invokeFirst = function (fn, receiver, arg) {
this._normalQueue.unshift(fn, receiver, arg);
this._queueTick();
};
Async.prototype._drainQueue = function(queue) {
while (queue.length() > 0) {
var fn = queue.shift();
if (typeof fn !== "function") {
fn._settlePromises();
continue;
}
var receiver = queue.shift();
var arg = queue.shift();
fn.call(receiver, arg);
}
};
Async.prototype._drainQueues = function () {
this._drainQueue(this._normalQueue);
this._reset();
this._drainQueue(this._lateQueue);
};
Async.prototype._queueTick = function () {
if (!this._isTickUsed) {
this._isTickUsed = true;
this._schedule(this.drainQueues);
}
};
Async.prototype._reset = function () {
this._isTickUsed = false;
};
module.exports = new Async();
module.exports.firstLineError = firstLineError;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL, tryConvertToPromise) {
var rejectThis = function(_, e) {
this._reject(e);
};
var targetRejected = function(e, context) {
context.promiseRejectionQueued = true;
context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
};
var bindingResolved = function(thisArg, context) {
if (this._isPending()) {
this._resolveCallback(context.target);
}
};
var bindingRejected = function(e, context) {
if (!context.promiseRejectionQueued) this._reject(e);
};
Promise.prototype.bind = function (thisArg) {
var maybePromise = tryConvertToPromise(thisArg);
var ret = new Promise(INTERNAL);
ret._propagateFrom(this, 1);
var target = this._target();
ret._setBoundTo(maybePromise);
if (maybePromise instanceof Promise) {
var context = {
promiseRejectionQueued: false,
promise: ret,
target: target,
bindingPromise: maybePromise
};
target._then(INTERNAL, targetRejected, ret._progress, ret, context);
maybePromise._then(
bindingResolved, bindingRejected, ret._progress, ret, context);
} else {
ret._resolveCallback(target);
}
return ret;
};
Promise.prototype._setBoundTo = function (obj) {
if (obj !== undefined) {
this._bitField = this._bitField | 131072;
this._boundTo = obj;
} else {
this._bitField = this._bitField & (~131072);
}
};
Promise.prototype._isBound = function () {
return (this._bitField & 131072) === 131072;
};
Promise.bind = function (thisArg, value) {
var maybePromise = tryConvertToPromise(thisArg);
var ret = new Promise(INTERNAL);
ret._setBoundTo(maybePromise);
if (maybePromise instanceof Promise) {
maybePromise._then(function() {
ret._resolveCallback(value);
}, ret._reject, ret._progress, ret, null);
} else {
ret._resolveCallback(value);
}
return ret;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 1 1 1 1 1 1 | "use strict"; var old; Eif (typeof Promise !== "undefined") old = Promise; function noConflict() { try { if (Promise === bluebird) Promise = old; } catch (e) {} return bluebird; } var bluebird = require("./promise.js")(); bluebird.noConflict = noConflict; module.exports = bluebird; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict"; var cr = Object.create; Eif (cr) { var callerCache = cr(null); var getterCache = cr(null); callerCache[" size"] = getterCache[" size"] = 0; } module.exports = function(Promise) { var util = require("./util.js"); var canEvaluate = util.canEvaluate; var isIdentifier = util.isIdentifier; var getMethodCaller; var getGetter; Eif (!false) { var makeMethodCaller = function (methodName) { return new Function("ensureMethod", " \n\ return function(obj) { \n\ 'use strict' \n\ var len = this.length; \n\ ensureMethod(obj, 'methodName'); \n\ switch(len) { \n\ case 1: return obj.methodName(this[0]); \n\ case 2: return obj.methodName(this[0], this[1]); \n\ case 3: return obj.methodName(this[0], this[1], this[2]); \n\ case 0: return obj.methodName(); \n\ default: \n\ return obj.methodName.apply(obj, this); \n\ } \n\ }; \n\ ".replace(/methodName/g, methodName))(ensureMethod); }; var makeGetter = function (propertyName) { return new Function("obj", " \n\ 'use strict'; \n\ return obj.propertyName; \n\ ".replace("propertyName", propertyName)); }; var getCompiled = function(name, compiler, cache) { var ret = cache[name]; if (typeof ret !== "function") { if (!isIdentifier(name)) { return null; } ret = compiler(name); cache[name] = ret; cache[" size"]++; if (cache[" size"] > 512) { var keys = Object.keys(cache); for (var i = 0; i < 256; ++i) delete cache[keys[i]]; cache[" size"] = keys.length - 256; } } return ret; }; getMethodCaller = function(name) { return getCompiled(name, makeMethodCaller, callerCache); }; getGetter = function(name) { return getCompiled(name, makeGetter, getterCache); }; } function ensureMethod(obj, methodName) { var fn; if (obj != null) fn = obj[methodName]; if (typeof fn !== "function") { var message = "Object " + util.classString(obj) + " has no method '" + util.toString(methodName) + "'"; throw new Promise.TypeError(message); } return fn; } function caller(obj) { var methodName = this.pop(); var fn = ensureMethod(obj, methodName); return fn.apply(obj, this); } Promise.prototype.call = function (methodName) { var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];} if (!false) { if (canEvaluate) { var maybeCaller = getMethodCaller(methodName); if (maybeCaller !== null) { return this._then( maybeCaller, undefined, undefined, args, undefined); } } } args.push(methodName); return this._then(caller, undefined, undefined, args, undefined); }; function namedGetter(obj) { return obj[this]; } function indexedGetter(obj) { var index = +this; if (index < 0) index = Math.max(0, index + obj.length); return obj[index]; } Promise.prototype.get = function (propertyName) { var isIndex = (typeof propertyName === "number"); var getter; if (!isIndex) { if (canEvaluate) { var maybeGetter = getGetter(propertyName); getter = maybeGetter !== null ? maybeGetter : namedGetter; } else { getter = namedGetter; } } else { getter = indexedGetter; } return this._then(getter, undefined, undefined, propertyName, undefined); }; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise) {
var errors = require("./errors.js");
var async = require("./async.js");
var CancellationError = errors.CancellationError;
Promise.prototype._cancel = function (reason) {
if (!this.isCancellable()) return this;
var parent;
var promiseToReject = this;
while ((parent = promiseToReject._cancellationParent) !== undefined &&
parent.isCancellable()) {
promiseToReject = parent;
}
this._unsetCancellable();
promiseToReject._target()._rejectCallback(reason, false, true);
};
Promise.prototype.cancel = function (reason) {
if (!this.isCancellable()) return this;
if (reason === undefined) reason = new CancellationError();
async.invokeLater(this._cancel, this, reason);
return this;
};
Promise.prototype.cancellable = function () {
if (this._cancellable()) return this;
async.enableTrampoline();
this._setCancellable();
this._cancellationParent = undefined;
return this;
};
Promise.prototype.uncancellable = function () {
var ret = this.then();
ret._unsetCancellable();
return ret;
};
Promise.prototype.fork = function (didFulfill, didReject, didProgress) {
var ret = this._then(didFulfill, didReject, didProgress,
undefined, undefined);
ret._setCancellable();
ret._cancellationParent = undefined;
return ret;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 4 4 2 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function() {
var async = require("./async.js");
var util = require("./util.js");
var bluebirdFramePattern =
/[\\\/]bluebird[\\\/]js[\\\/](main|debug|zalgo|instrumented)/;
var stackFramePattern = null;
var formatStack = null;
var indentStackFrames = false;
var warn;
function CapturedTrace(parent) {
this._parent = parent;
var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
captureStackTrace(this, CapturedTrace);
if (length > 32) this.uncycle();
}
util.inherits(CapturedTrace, Error);
CapturedTrace.prototype.uncycle = function() {
var length = this._length;
if (length < 2) return;
var nodes = [];
var stackToIndex = {};
for (var i = 0, node = this; node !== undefined; ++i) {
nodes.push(node);
node = node._parent;
}
length = this._length = i;
for (var i = length - 1; i >= 0; --i) {
var stack = nodes[i].stack;
if (stackToIndex[stack] === undefined) {
stackToIndex[stack] = i;
}
}
for (var i = 0; i < length; ++i) {
var currentStack = nodes[i].stack;
var index = stackToIndex[currentStack];
if (index !== undefined && index !== i) {
if (index > 0) {
nodes[index - 1]._parent = undefined;
nodes[index - 1]._length = 1;
}
nodes[i]._parent = undefined;
nodes[i]._length = 1;
var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
if (index < length - 1) {
cycleEdgeNode._parent = nodes[index + 1];
cycleEdgeNode._parent.uncycle();
cycleEdgeNode._length =
cycleEdgeNode._parent._length + 1;
} else {
cycleEdgeNode._parent = undefined;
cycleEdgeNode._length = 1;
}
var currentChildLength = cycleEdgeNode._length + 1;
for (var j = i - 2; j >= 0; --j) {
nodes[j]._length = currentChildLength;
currentChildLength++;
}
return;
}
}
};
CapturedTrace.prototype.parent = function() {
return this._parent;
};
CapturedTrace.prototype.hasParent = function() {
return this._parent !== undefined;
};
CapturedTrace.prototype.attachExtraTrace = function(error) {
if (error.__stackCleaned__) return;
this.uncycle();
var parsed = CapturedTrace.parseStackAndMessage(error);
var message = parsed.message;
var stacks = [parsed.stack];
var trace = this;
while (trace !== undefined) {
stacks.push(cleanStack(trace.stack.split("\n")));
trace = trace._parent;
}
removeCommonRoots(stacks);
removeDuplicateOrEmptyJumps(stacks);
util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
util.notEnumerableProp(error, "__stackCleaned__", true);
};
function reconstructStack(message, stacks) {
for (var i = 0; i < stacks.length - 1; ++i) {
stacks[i].push("From previous event:");
stacks[i] = stacks[i].join("\n");
}
if (i < stacks.length) {
stacks[i] = stacks[i].join("\n");
}
return message + "\n" + stacks.join("\n");
}
function removeDuplicateOrEmptyJumps(stacks) {
for (var i = 0; i < stacks.length; ++i) {
if (stacks[i].length === 0 ||
((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) {
stacks.splice(i, 1);
i--;
}
}
}
function removeCommonRoots(stacks) {
var current = stacks[0];
for (var i = 1; i < stacks.length; ++i) {
var prev = stacks[i];
var currentLastIndex = current.length - 1;
var currentLastLine = current[currentLastIndex];
var commonRootMeetPoint = -1;
for (var j = prev.length - 1; j >= 0; --j) {
if (prev[j] === currentLastLine) {
commonRootMeetPoint = j;
break;
}
}
for (var j = commonRootMeetPoint; j >= 0; --j) {
var line = prev[j];
if (current[currentLastIndex] === line) {
current.pop();
currentLastIndex--;
} else {
break;
}
}
current = prev;
}
}
function cleanStack(stack) {
var ret = [];
for (var i = 0; i < stack.length; ++i) {
var line = stack[i];
var isTraceLine = stackFramePattern.test(line) ||
" (No stack trace)" === line;
var isInternalFrame = isTraceLine && shouldIgnore(line);
if (isTraceLine && !isInternalFrame) {
if (indentStackFrames && line.charAt(0) !== " ") {
line = " " + line;
}
ret.push(line);
}
}
return ret;
}
function stackFramesAsArray(error) {
var stack = error.stack.replace(/\s+$/g, "").split("\n");
for (var i = 0; i < stack.length; ++i) {
var line = stack[i];
if (" (No stack trace)" === line || stackFramePattern.test(line)) {
break;
}
}
if (i > 0) {
stack = stack.slice(i);
}
return stack;
}
CapturedTrace.parseStackAndMessage = function(error) {
var stack = error.stack;
var message = error.toString();
stack = typeof stack === "string" && stack.length > 0
? stackFramesAsArray(error) : [" (No stack trace)"];
return {
message: message,
stack: cleanStack(stack)
};
};
CapturedTrace.formatAndLogError = function(error, title) {
if (typeof console !== "undefined") {
var message;
if (typeof error === "object" || typeof error === "function") {
var stack = error.stack;
message = title + formatStack(stack, error);
} else {
message = title + String(error);
}
if (typeof warn === "function") {
warn(message);
} else if (typeof console.log === "function" ||
typeof console.log === "object") {
console.log(message);
}
}
};
CapturedTrace.unhandledRejection = function (reason) {
CapturedTrace.formatAndLogError(reason, "^--- With additional stack trace: ");
};
CapturedTrace.isSupported = function () {
return typeof captureStackTrace === "function";
};
CapturedTrace.fireRejectionEvent =
function(name, localHandler, reason, promise) {
var localEventFired = false;
try {
if (typeof localHandler === "function") {
localEventFired = true;
if (name === "rejectionHandled") {
localHandler(promise);
} else {
localHandler(reason, promise);
}
}
} catch (e) {
async.throwLater(e);
}
var globalEventFired = false;
try {
globalEventFired = fireGlobalEvent(name, reason, promise);
} catch (e) {
globalEventFired = true;
async.throwLater(e);
}
var domEventFired = false;
if (fireDomEvent) {
try {
domEventFired = fireDomEvent(name.toLowerCase(), {
reason: reason,
promise: promise
});
} catch (e) {
domEventFired = true;
async.throwLater(e);
}
}
if (!globalEventFired && !localEventFired && !domEventFired &&
name === "unhandledRejection") {
CapturedTrace.formatAndLogError(reason, "Unhandled rejection ");
}
};
function formatNonError(obj) {
var str;
if (typeof obj === "function") {
str = "[function " +
(obj.name || "anonymous") +
"]";
} else {
str = obj.toString();
var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
if (ruselessToString.test(str)) {
try {
var newStr = JSON.stringify(obj);
str = newStr;
}
catch(e) {
}
}
if (str.length === 0) {
str = "(empty array)";
}
}
return ("(<" + snip(str) + ">, no stack trace)");
}
function snip(str) {
var maxChars = 41;
if (str.length < maxChars) {
return str;
}
return str.substr(0, maxChars - 3) + "...";
}
var shouldIgnore = function() { return false; };
var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/;
function parseLineInfo(line) {
var matches = line.match(parseLineInfoRegex);
if (matches) {
return {
fileName: matches[1],
line: parseInt(matches[2], 10)
};
}
}
CapturedTrace.setBounds = function(firstLineError, lastLineError) {
Iif (!CapturedTrace.isSupported()) return;
var firstStackLines = firstLineError.stack.split("\n");
var lastStackLines = lastLineError.stack.split("\n");
var firstIndex = -1;
var lastIndex = -1;
var firstFileName;
var lastFileName;
for (var i = 0; i < firstStackLines.length; ++i) {
var result = parseLineInfo(firstStackLines[i]);
if (result) {
firstFileName = result.fileName;
firstIndex = result.line;
break;
}
}
for (var i = 0; i < lastStackLines.length; ++i) {
var result = parseLineInfo(lastStackLines[i]);
if (result) {
lastFileName = result.fileName;
lastIndex = result.line;
break;
}
}
Eif (firstIndex < 0 || lastIndex < 0 || !firstFileName || !lastFileName ||
firstFileName !== lastFileName || firstIndex >= lastIndex) {
return;
}
shouldIgnore = function(line) {
if (bluebirdFramePattern.test(line)) return true;
var info = parseLineInfo(line);
if (info) {
if (info.fileName === firstFileName &&
(firstIndex <= info.line && info.line <= lastIndex)) {
return true;
}
}
return false;
};
};
var captureStackTrace = (function stackDetection() {
var v8stackFramePattern = /^\s*at\s*/;
var v8stackFormatter = function(stack, error) {
if (typeof stack === "string") return stack;
if (error.name !== undefined &&
error.message !== undefined) {
return error.toString();
}
return formatNonError(error);
};
Eif (typeof Error.stackTraceLimit === "number" &&
typeof Error.captureStackTrace === "function") {
Error.stackTraceLimit = Error.stackTraceLimit + 6;
stackFramePattern = v8stackFramePattern;
formatStack = v8stackFormatter;
var captureStackTrace = Error.captureStackTrace;
shouldIgnore = function(line) {
return bluebirdFramePattern.test(line);
};
return function(receiver, ignoreUntil) {
Error.stackTraceLimit = Error.stackTraceLimit + 6;
captureStackTrace(receiver, ignoreUntil);
Error.stackTraceLimit = Error.stackTraceLimit - 6;
};
}
var err = new Error();
if (typeof err.stack === "string" &&
err.stack.split("\n")[0].indexOf("stackDetection@") >= 0) {
stackFramePattern = /@/;
formatStack = v8stackFormatter;
indentStackFrames = true;
return function captureStackTrace(o) {
o.stack = new Error().stack;
};
}
var hasStackAfterThrow;
try { throw new Error(); }
catch(e) {
hasStackAfterThrow = ("stack" in e);
}
if (!("stack" in err) && hasStackAfterThrow &&
typeof Error.stackTraceLimit === "number") {
stackFramePattern = v8stackFramePattern;
formatStack = v8stackFormatter;
return function captureStackTrace(o) {
Error.stackTraceLimit = Error.stackTraceLimit + 6;
try { throw new Error(); }
catch(e) { o.stack = e.stack; }
Error.stackTraceLimit = Error.stackTraceLimit - 6;
};
}
formatStack = function(stack, error) {
if (typeof stack === "string") return stack;
if ((typeof error === "object" ||
typeof error === "function") &&
error.name !== undefined &&
error.message !== undefined) {
return error.toString();
}
return formatNonError(error);
};
return null;
})([]);
var fireDomEvent;
var fireGlobalEvent = (function() {
Eif (util.isNode) {
return function(name, reason, promise) {
if (name === "rejectionHandled") {
return process.emit(name, promise);
} else {
return process.emit(name, reason, promise);
}
};
} else {
var customEventWorks = false;
var anyEventWorks = true;
try {
var ev = new self.CustomEvent("test");
customEventWorks = ev instanceof CustomEvent;
} catch (e) {}
if (!customEventWorks) {
try {
var event = document.createEvent("CustomEvent");
event.initCustomEvent("testingtheevent", false, true, {});
self.dispatchEvent(event);
} catch (e) {
anyEventWorks = false;
}
}
if (anyEventWorks) {
fireDomEvent = function(type, detail) {
var event;
if (customEventWorks) {
event = new self.CustomEvent(type, {
detail: detail,
bubbles: false,
cancelable: true
});
} else if (self.dispatchEvent) {
event = document.createEvent("CustomEvent");
event.initCustomEvent(type, false, true, detail);
}
return event ? !self.dispatchEvent(event) : false;
};
}
var toWindowMethodNameMap = {};
toWindowMethodNameMap["unhandledRejection"] = ("on" +
"unhandledRejection").toLowerCase();
toWindowMethodNameMap["rejectionHandled"] = ("on" +
"rejectionHandled").toLowerCase();
return function(name, reason, promise) {
var methodName = toWindowMethodNameMap[name];
var method = self[methodName];
if (!method) return false;
if (name === "rejectionHandled") {
method.call(self, promise);
} else {
method.call(self, reason, promise);
}
return true;
};
}
})();
Eif (typeof console !== "undefined" && typeof console.warn !== "undefined") {
warn = function (message) {
console.warn(message);
};
Eif (util.isNode && process.stderr.isTTY) {
warn = function(message) {
process.stderr.write("\u001b[31m" + message + "\u001b[39m\n");
};
} else if (!util.isNode && typeof (new Error().stack) === "string") {
warn = function(message) {
console.warn("%c" + message, "color: red");
};
}
}
return CapturedTrace;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(NEXT_FILTER) {
var util = require("./util.js");
var errors = require("./errors.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
var keys = require("./es5.js").keys;
var TypeError = errors.TypeError;
function CatchFilter(instances, callback, promise) {
this._instances = instances;
this._callback = callback;
this._promise = promise;
}
function safePredicate(predicate, e) {
var safeObject = {};
var retfilter = tryCatch(predicate).call(safeObject, e);
if (retfilter === errorObj) return retfilter;
var safeKeys = keys(safeObject);
if (safeKeys.length) {
errorObj.e = new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a");
return errorObj;
}
return retfilter;
}
CatchFilter.prototype.doFilter = function (e) {
var cb = this._callback;
var promise = this._promise;
var boundTo = promise._boundValue();
for (var i = 0, len = this._instances.length; i < len; ++i) {
var item = this._instances[i];
var itemIsErrorType = item === Error ||
(item != null && item.prototype instanceof Error);
if (itemIsErrorType && e instanceof item) {
var ret = tryCatch(cb).call(boundTo, e);
if (ret === errorObj) {
NEXT_FILTER.e = ret.e;
return NEXT_FILTER;
}
return ret;
} else if (typeof item === "function" && !itemIsErrorType) {
var shouldHandle = safePredicate(item, e);
if (shouldHandle === errorObj) {
e = errorObj.e;
break;
} else if (shouldHandle) {
var ret = tryCatch(cb).call(boundTo, e);
if (ret === errorObj) {
NEXT_FILTER.e = ret.e;
return NEXT_FILTER;
}
return ret;
}
}
}
NEXT_FILTER.e = e;
return NEXT_FILTER;
};
return CatchFilter;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, CapturedTrace, isDebugging) {
var contextStack = [];
function Context() {
this._trace = new CapturedTrace(peekContext());
}
Context.prototype._pushContext = function () {
if (!isDebugging()) return;
if (this._trace !== undefined) {
contextStack.push(this._trace);
}
};
Context.prototype._popContext = function () {
if (!isDebugging()) return;
if (this._trace !== undefined) {
contextStack.pop();
}
};
function createContext() {
if (isDebugging()) return new Context();
}
function peekContext() {
var lastIndex = contextStack.length - 1;
if (lastIndex >= 0) {
return contextStack[lastIndex];
}
return undefined;
}
Promise.prototype._peekContext = peekContext;
Promise.prototype._pushContext = Context.prototype._pushContext;
Promise.prototype._popContext = Context.prototype._popContext;
return createContext;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, CapturedTrace) {
var getDomain = Promise._getDomain;
var async = require("./async.js");
var Warning = require("./errors.js").Warning;
var util = require("./util.js");
var canAttachTrace = util.canAttachTrace;
var unhandledRejectionHandled;
var possiblyUnhandledRejection;
var debugging = false || (util.isNode &&
(!!process.env["BLUEBIRD_DEBUG"] ||
process.env["NODE_ENV"] === "development"));
Iif (util.isNode && process.env["BLUEBIRD_DEBUG"] == 0) debugging = false;
Iif (debugging) {
async.disableTrampolineIfNecessary();
}
Promise.prototype._ignoreRejections = function() {
this._unsetRejectionIsUnhandled();
this._bitField = this._bitField | 16777216;
};
Promise.prototype._ensurePossibleRejectionHandled = function () {
if ((this._bitField & 16777216) !== 0) return;
this._setRejectionIsUnhandled();
async.invokeLater(this._notifyUnhandledRejection, this, undefined);
};
Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
CapturedTrace.fireRejectionEvent("rejectionHandled",
unhandledRejectionHandled, undefined, this);
};
Promise.prototype._notifyUnhandledRejection = function () {
if (this._isRejectionUnhandled()) {
var reason = this._getCarriedStackTrace() || this._settledValue;
this._setUnhandledRejectionIsNotified();
CapturedTrace.fireRejectionEvent("unhandledRejection",
possiblyUnhandledRejection, reason, this);
}
};
Promise.prototype._setUnhandledRejectionIsNotified = function () {
this._bitField = this._bitField | 524288;
};
Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
this._bitField = this._bitField & (~524288);
};
Promise.prototype._isUnhandledRejectionNotified = function () {
return (this._bitField & 524288) > 0;
};
Promise.prototype._setRejectionIsUnhandled = function () {
this._bitField = this._bitField | 2097152;
};
Promise.prototype._unsetRejectionIsUnhandled = function () {
this._bitField = this._bitField & (~2097152);
if (this._isUnhandledRejectionNotified()) {
this._unsetUnhandledRejectionIsNotified();
this._notifyUnhandledRejectionIsHandled();
}
};
Promise.prototype._isRejectionUnhandled = function () {
return (this._bitField & 2097152) > 0;
};
Promise.prototype._setCarriedStackTrace = function (capturedTrace) {
this._bitField = this._bitField | 1048576;
this._fulfillmentHandler0 = capturedTrace;
};
Promise.prototype._isCarryingStackTrace = function () {
return (this._bitField & 1048576) > 0;
};
Promise.prototype._getCarriedStackTrace = function () {
return this._isCarryingStackTrace()
? this._fulfillmentHandler0
: undefined;
};
Promise.prototype._captureStackTrace = function () {
if (debugging) {
this._trace = new CapturedTrace(this._peekContext());
}
return this;
};
Promise.prototype._attachExtraTrace = function (error, ignoreSelf) {
if (debugging && canAttachTrace(error)) {
var trace = this._trace;
if (trace !== undefined) {
if (ignoreSelf) trace = trace._parent;
}
if (trace !== undefined) {
trace.attachExtraTrace(error);
} else if (!error.__stackCleaned__) {
var parsed = CapturedTrace.parseStackAndMessage(error);
util.notEnumerableProp(error, "stack",
parsed.message + "\n" + parsed.stack.join("\n"));
util.notEnumerableProp(error, "__stackCleaned__", true);
}
}
};
Promise.prototype._warn = function(message) {
var warning = new Warning(message);
var ctx = this._peekContext();
if (ctx) {
ctx.attachExtraTrace(warning);
} else {
var parsed = CapturedTrace.parseStackAndMessage(warning);
warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
}
CapturedTrace.formatAndLogError(warning, "");
};
Promise.onPossiblyUnhandledRejection = function (fn) {
var domain = getDomain();
possiblyUnhandledRejection =
typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
: undefined;
};
Promise.onUnhandledRejectionHandled = function (fn) {
var domain = getDomain();
unhandledRejectionHandled =
typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
: undefined;
};
Promise.longStackTraces = function () {
if (async.haveItemsQueued() &&
debugging === false
) {
throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/DT1qyG\u000a");
}
debugging = CapturedTrace.isSupported();
if (debugging) {
async.disableTrampolineIfNecessary();
}
};
Promise.hasLongStackTraces = function () {
return debugging && CapturedTrace.isSupported();
};
Iif (!CapturedTrace.isSupported()) {
Promise.longStackTraces = function(){};
debugging = false;
}
return function() {
return debugging;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1 1 1 1 1 1 1 1 1 1 | "use strict";
var util = require("./util.js");
var isPrimitive = util.isPrimitive;
module.exports = function(Promise) {
var returner = function () {
return this;
};
var thrower = function () {
throw this;
};
var returnUndefined = function() {};
var throwUndefined = function() {
throw undefined;
};
var wrapper = function (value, action) {
if (action === 1) {
return function () {
throw value;
};
} else if (action === 2) {
return function () {
return value;
};
}
};
Promise.prototype["return"] =
Promise.prototype.thenReturn = function (value) {
if (value === undefined) return this.then(returnUndefined);
if (isPrimitive(value)) {
return this._then(
wrapper(value, 2),
undefined,
undefined,
undefined,
undefined
);
} else if (value instanceof Promise) {
value._ignoreRejections();
}
return this._then(returner, undefined, undefined, value, undefined);
};
Promise.prototype["throw"] =
Promise.prototype.thenThrow = function (reason) {
if (reason === undefined) return this.then(throwUndefined);
if (isPrimitive(reason)) {
return this._then(
wrapper(reason, 1),
undefined,
undefined,
undefined,
undefined
);
}
return this._then(thrower, undefined, undefined, reason, undefined);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL) {
var PromiseReduce = Promise.reduce;
Promise.prototype.each = function (fn) {
return PromiseReduce(this, fn, null, INTERNAL);
};
Promise.each = function (promises, fn) {
return PromiseReduce(promises, fn, null, INTERNAL);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 1 1 1 1 1 1 1 4 4 1 1 1 1 1 1 1 1 1 1 17 17 1 1 1 1 1 1 1 1 1 | "use strict";
var es5 = require("./es5.js");
var Objectfreeze = es5.freeze;
var util = require("./util.js");
var inherits = util.inherits;
var notEnumerableProp = util.notEnumerableProp;
function subError(nameProperty, defaultMessage) {
function SubError(message) {
if (!(this instanceof SubError)) return new SubError(message);
notEnumerableProp(this, "message",
typeof message === "string" ? message : defaultMessage);
notEnumerableProp(this, "name", nameProperty);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
} else {
Error.call(this);
}
}
inherits(SubError, Error);
return SubError;
}
var _TypeError, _RangeError;
var Warning = subError("Warning", "warning");
var CancellationError = subError("CancellationError", "cancellation error");
var TimeoutError = subError("TimeoutError", "timeout error");
var AggregateError = subError("AggregateError", "aggregate error");
try {
_TypeError = TypeError;
_RangeError = RangeError;
} catch(e) {
_TypeError = subError("TypeError", "type error");
_RangeError = subError("RangeError", "range error");
}
var methods = ("join pop push shift unshift slice filter forEach some " +
"every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" ");
for (var i = 0; i < methods.length; ++i) {
Eif (typeof Array.prototype[methods[i]] === "function") {
AggregateError.prototype[methods[i]] = Array.prototype[methods[i]];
}
}
es5.defineProperty(AggregateError.prototype, "length", {
value: 0,
configurable: false,
writable: true,
enumerable: true
});
AggregateError.prototype["isOperational"] = true;
var level = 0;
AggregateError.prototype.toString = function() {
var indent = Array(level * 4 + 1).join(" ");
var ret = "\n" + indent + "AggregateError of:" + "\n";
level++;
indent = Array(level * 4 + 1).join(" ");
for (var i = 0; i < this.length; ++i) {
var str = this[i] === this ? "[Circular AggregateError]" : this[i] + "";
var lines = str.split("\n");
for (var j = 0; j < lines.length; ++j) {
lines[j] = indent + lines[j];
}
str = lines.join("\n");
ret += str + "\n";
}
level--;
return ret;
};
function OperationalError(message) {
if (!(this instanceof OperationalError))
return new OperationalError(message);
notEnumerableProp(this, "name", "OperationalError");
notEnumerableProp(this, "message", message);
this.cause = message;
this["isOperational"] = true;
if (message instanceof Error) {
notEnumerableProp(this, "message", message.message);
notEnumerableProp(this, "stack", message.stack);
} else if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
inherits(OperationalError, Error);
var errorTypes = Error["__BluebirdErrorTypes__"];
Iif (!errorTypes) {
errorTypes = Objectfreeze({
CancellationError: CancellationError,
TimeoutError: TimeoutError,
OperationalError: OperationalError,
RejectionError: OperationalError,
AggregateError: AggregateError
});
notEnumerableProp(Error, "__BluebirdErrorTypes__", errorTypes);
}
module.exports = {
Error: Error,
TypeError: _TypeError,
RangeError: _RangeError,
CancellationError: errorTypes.CancellationError,
OperationalError: errorTypes.OperationalError,
TimeoutError: errorTypes.TimeoutError,
AggregateError: errorTypes.AggregateError,
Warning: Warning
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 1 1 1 1 | var isES5 = (function(){
"use strict";
return this === undefined;
})();
Eif (isES5) {
module.exports = {
freeze: Object.freeze,
defineProperty: Object.defineProperty,
getDescriptor: Object.getOwnPropertyDescriptor,
keys: Object.keys,
names: Object.getOwnPropertyNames,
getPrototypeOf: Object.getPrototypeOf,
isArray: Array.isArray,
isES5: isES5,
propertyIsWritable: function(obj, prop) {
var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
return !!(!descriptor || descriptor.writable || descriptor.set);
}
};
} else {
var has = {}.hasOwnProperty;
var str = {}.toString;
var proto = {}.constructor.prototype;
var ObjectKeys = function (o) {
var ret = [];
for (var key in o) {
if (has.call(o, key)) {
ret.push(key);
}
}
return ret;
};
var ObjectGetDescriptor = function(o, key) {
return {value: o[key]};
};
var ObjectDefineProperty = function (o, key, desc) {
o[key] = desc.value;
return o;
};
var ObjectFreeze = function (obj) {
return obj;
};
var ObjectGetPrototypeOf = function (obj) {
try {
return Object(obj).constructor.prototype;
}
catch (e) {
return proto;
}
};
var ArrayIsArray = function (obj) {
try {
return str.call(obj) === "[object Array]";
}
catch(e) {
return false;
}
};
module.exports = {
isArray: ArrayIsArray,
keys: ObjectKeys,
names: ObjectKeys,
defineProperty: ObjectDefineProperty,
getDescriptor: ObjectGetDescriptor,
freeze: ObjectFreeze,
getPrototypeOf: ObjectGetPrototypeOf,
isES5: isES5,
propertyIsWritable: function() {
return true;
}
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL) {
var PromiseMap = Promise.map;
Promise.prototype.filter = function (fn, options) {
return PromiseMap(this, fn, options, INTERNAL);
};
Promise.filter = function (promises, fn, options) {
return PromiseMap(promises, fn, options, INTERNAL);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, NEXT_FILTER, tryConvertToPromise) {
var util = require("./util.js");
var isPrimitive = util.isPrimitive;
var thrower = util.thrower;
function returnThis() {
return this;
}
function throwThis() {
throw this;
}
function return$(r) {
return function() {
return r;
};
}
function throw$(r) {
return function() {
throw r;
};
}
function promisedFinally(ret, reasonOrValue, isFulfilled) {
var then;
if (isPrimitive(reasonOrValue)) {
then = isFulfilled ? return$(reasonOrValue) : throw$(reasonOrValue);
} else {
then = isFulfilled ? returnThis : throwThis;
}
return ret._then(then, thrower, undefined, reasonOrValue, undefined);
}
function finallyHandler(reasonOrValue) {
var promise = this.promise;
var handler = this.handler;
var ret = promise._isBound()
? handler.call(promise._boundValue())
: handler();
if (ret !== undefined) {
var maybePromise = tryConvertToPromise(ret, promise);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
return promisedFinally(maybePromise, reasonOrValue,
promise.isFulfilled());
}
}
if (promise.isRejected()) {
NEXT_FILTER.e = reasonOrValue;
return NEXT_FILTER;
} else {
return reasonOrValue;
}
}
function tapHandler(value) {
var promise = this.promise;
var handler = this.handler;
var ret = promise._isBound()
? handler.call(promise._boundValue(), value)
: handler(value);
if (ret !== undefined) {
var maybePromise = tryConvertToPromise(ret, promise);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
return promisedFinally(maybePromise, value, true);
}
}
return value;
}
Promise.prototype._passThroughHandler = function (handler, isFinally) {
if (typeof handler !== "function") return this.then();
var promiseAndHandler = {
promise: this,
handler: handler
};
return this._then(
isFinally ? finallyHandler : tapHandler,
isFinally ? finallyHandler : undefined, undefined,
promiseAndHandler, undefined);
};
Promise.prototype.lastly =
Promise.prototype["finally"] = function (handler) {
return this._passThroughHandler(handler, true);
};
Promise.prototype.tap = function (handler) {
return this._passThroughHandler(handler, false);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise,
apiRejection,
INTERNAL,
tryConvertToPromise) {
var errors = require("./errors.js");
var TypeError = errors.TypeError;
var util = require("./util.js");
var errorObj = util.errorObj;
var tryCatch = util.tryCatch;
var yieldHandlers = [];
function promiseFromYieldHandler(value, yieldHandlers, traceParent) {
for (var i = 0; i < yieldHandlers.length; ++i) {
traceParent._pushContext();
var result = tryCatch(yieldHandlers[i])(value);
traceParent._popContext();
if (result === errorObj) {
traceParent._pushContext();
var ret = Promise.reject(errorObj.e);
traceParent._popContext();
return ret;
}
var maybePromise = tryConvertToPromise(result, traceParent);
if (maybePromise instanceof Promise) return maybePromise;
}
return null;
}
function PromiseSpawn(generatorFunction, receiver, yieldHandler, stack) {
var promise = this._promise = new Promise(INTERNAL);
promise._captureStackTrace();
this._stack = stack;
this._generatorFunction = generatorFunction;
this._receiver = receiver;
this._generator = undefined;
this._yieldHandlers = typeof yieldHandler === "function"
? [yieldHandler].concat(yieldHandlers)
: yieldHandlers;
}
PromiseSpawn.prototype.promise = function () {
return this._promise;
};
PromiseSpawn.prototype._run = function () {
this._generator = this._generatorFunction.call(this._receiver);
this._receiver =
this._generatorFunction = undefined;
this._next(undefined);
};
PromiseSpawn.prototype._continue = function (result) {
if (result === errorObj) {
return this._promise._rejectCallback(result.e, false, true);
}
var value = result.value;
if (result.done === true) {
this._promise._resolveCallback(value);
} else {
var maybePromise = tryConvertToPromise(value, this._promise);
if (!(maybePromise instanceof Promise)) {
maybePromise =
promiseFromYieldHandler(maybePromise,
this._yieldHandlers,
this._promise);
if (maybePromise === null) {
this._throw(
new TypeError(
"A value %s was yielded that could not be treated as a promise\u000a\u000a See http://goo.gl/4Y4pDk\u000a\u000a".replace("%s", value) +
"From coroutine:\u000a" +
this._stack.split("\n").slice(1, -7).join("\n")
)
);
return;
}
}
maybePromise._then(
this._next,
this._throw,
undefined,
this,
null
);
}
};
PromiseSpawn.prototype._throw = function (reason) {
this._promise._attachExtraTrace(reason);
this._promise._pushContext();
var result = tryCatch(this._generator["throw"])
.call(this._generator, reason);
this._promise._popContext();
this._continue(result);
};
PromiseSpawn.prototype._next = function (value) {
this._promise._pushContext();
var result = tryCatch(this._generator.next).call(this._generator, value);
this._promise._popContext();
this._continue(result);
};
Promise.coroutine = function (generatorFunction, options) {
if (typeof generatorFunction !== "function") {
throw new TypeError("generatorFunction must be a function\u000a\u000a See http://goo.gl/6Vqhm0\u000a");
}
var yieldHandler = Object(options).yieldHandler;
var PromiseSpawn$ = PromiseSpawn;
var stack = new Error().stack;
return function () {
var generator = generatorFunction.apply(this, arguments);
var spawn = new PromiseSpawn$(undefined, undefined, yieldHandler,
stack);
spawn._generator = generator;
spawn._next(undefined);
return spawn.promise();
};
};
Promise.coroutine.addYieldHandler = function(fn) {
if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
yieldHandlers.push(fn);
};
Promise.spawn = function (generatorFunction) {
if (typeof generatorFunction !== "function") {
return apiRejection("generatorFunction must be a function\u000a\u000a See http://goo.gl/6Vqhm0\u000a");
}
var spawn = new PromiseSpawn(generatorFunction, this);
var ret = spawn.promise();
spawn._run(Promise.spawn);
return ret;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 1 1 1 1 1 1 1 1 1 5 1 5 15 5 1 1 1 5 5 1 1 1 1 1 | "use strict";
module.exports =
function(Promise, PromiseArray, tryConvertToPromise, INTERNAL) {
var util = require("./util.js");
var canEvaluate = util.canEvaluate;
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
var reject;
Eif (!false) {
Eif (canEvaluate) {
var thenCallback = function(i) {
return new Function("value", "holder", " \n\
'use strict'; \n\
holder.pIndex = value; \n\
holder.checkFulfillment(this); \n\
".replace(/Index/g, i));
};
var caller = function(count) {
var values = [];
for (var i = 1; i <= count; ++i) values.push("holder.p" + i);
return new Function("holder", " \n\
'use strict'; \n\
var callback = holder.fn; \n\
return callback(values); \n\
".replace(/values/g, values.join(", ")));
};
var thenCallbacks = [];
var callers = [undefined];
for (var i = 1; i <= 5; ++i) {
thenCallbacks.push(thenCallback(i));
callers.push(caller(i));
}
var Holder = function(total, fn) {
this.p1 = this.p2 = this.p3 = this.p4 = this.p5 = null;
this.fn = fn;
this.total = total;
this.now = 0;
};
Holder.prototype.callers = callers;
Holder.prototype.checkFulfillment = function(promise) {
var now = this.now;
now++;
var total = this.total;
if (now >= total) {
var handler = this.callers[total];
promise._pushContext();
var ret = tryCatch(handler)(this);
promise._popContext();
if (ret === errorObj) {
promise._rejectCallback(ret.e, false, true);
} else {
promise._resolveCallback(ret);
}
} else {
this.now = now;
}
};
var reject = function (reason) {
this._reject(reason);
};
}
}
Promise.join = function () {
var last = arguments.length - 1;
var fn;
if (last > 0 && typeof arguments[last] === "function") {
fn = arguments[last];
if (!false) {
if (last < 6 && canEvaluate) {
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
var holder = new Holder(last, fn);
var callbacks = thenCallbacks;
for (var i = 0; i < last; ++i) {
var maybePromise = tryConvertToPromise(arguments[i], ret);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
if (maybePromise._isPending()) {
maybePromise._then(callbacks[i], reject,
undefined, ret, holder);
} else if (maybePromise._isFulfilled()) {
callbacks[i].call(ret,
maybePromise._value(), holder);
} else {
ret._reject(maybePromise._reason());
}
} else {
callbacks[i].call(ret, maybePromise, holder);
}
}
return ret;
}
}
}
var $_len = arguments.length;var args = new Array($_len); for(var $_i = 0; $_i < $_len; ++$_i) {args[$_i] = arguments[$_i];}
if (fn) args.pop();
var ret = new PromiseArray(args).promise();
return fn !== undefined ? ret.spread(fn) : ret;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise,
PromiseArray,
apiRejection,
tryConvertToPromise,
INTERNAL) {
var getDomain = Promise._getDomain;
var async = require("./async.js");
var util = require("./util.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
var PENDING = {};
var EMPTY_ARRAY = [];
function MappingPromiseArray(promises, fn, limit, _filter) {
this.constructor$(promises);
this._promise._captureStackTrace();
var domain = getDomain();
this._callback = domain === null ? fn : domain.bind(fn);
this._preservedValues = _filter === INTERNAL
? new Array(this.length())
: null;
this._limit = limit;
this._inFlight = 0;
this._queue = limit >= 1 ? [] : EMPTY_ARRAY;
async.invoke(init, this, undefined);
}
util.inherits(MappingPromiseArray, PromiseArray);
function init() {this._init$(undefined, -2);}
MappingPromiseArray.prototype._init = function () {};
MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
var values = this._values;
var length = this.length();
var preservedValues = this._preservedValues;
var limit = this._limit;
if (values[index] === PENDING) {
values[index] = value;
if (limit >= 1) {
this._inFlight--;
this._drainQueue();
if (this._isResolved()) return;
}
} else {
if (limit >= 1 && this._inFlight >= limit) {
values[index] = value;
this._queue.push(index);
return;
}
if (preservedValues !== null) preservedValues[index] = value;
var callback = this._callback;
var receiver = this._promise._boundValue();
this._promise._pushContext();
var ret = tryCatch(callback).call(receiver, value, index, length);
this._promise._popContext();
if (ret === errorObj) return this._reject(ret.e);
var maybePromise = tryConvertToPromise(ret, this._promise);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
if (maybePromise._isPending()) {
if (limit >= 1) this._inFlight++;
values[index] = PENDING;
return maybePromise._proxyPromiseArray(this, index);
} else if (maybePromise._isFulfilled()) {
ret = maybePromise._value();
} else {
return this._reject(maybePromise._reason());
}
}
values[index] = ret;
}
var totalResolved = ++this._totalResolved;
if (totalResolved >= length) {
if (preservedValues !== null) {
this._filter(values, preservedValues);
} else {
this._resolve(values);
}
}
};
MappingPromiseArray.prototype._drainQueue = function () {
var queue = this._queue;
var limit = this._limit;
var values = this._values;
while (queue.length > 0 && this._inFlight < limit) {
if (this._isResolved()) return;
var index = queue.pop();
this._promiseFulfilled(values[index], index);
}
};
MappingPromiseArray.prototype._filter = function (booleans, values) {
var len = values.length;
var ret = new Array(len);
var j = 0;
for (var i = 0; i < len; ++i) {
if (booleans[i]) ret[j++] = values[i];
}
ret.length = j;
this._resolve(ret);
};
MappingPromiseArray.prototype.preservedValues = function () {
return this._preservedValues;
};
function map(promises, fn, options, _filter) {
var limit = typeof options === "object" && options !== null
? options.concurrency
: 0;
limit = typeof limit === "number" &&
isFinite(limit) && limit >= 1 ? limit : 0;
return new MappingPromiseArray(promises, fn, limit, _filter);
}
Promise.prototype.map = function (fn, options) {
if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
return map(this, fn, options, null).promise();
};
Promise.map = function (promises, fn, options, _filter) {
if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
return map(promises, fn, options, _filter).promise();
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1 1 1 1 1 1 | "use strict";
module.exports =
function(Promise, INTERNAL, tryConvertToPromise, apiRejection) {
var util = require("./util.js");
var tryCatch = util.tryCatch;
Promise.method = function (fn) {
if (typeof fn !== "function") {
throw new Promise.TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
}
return function () {
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
ret._pushContext();
var value = tryCatch(fn).apply(this, arguments);
ret._popContext();
ret._resolveFromSyncValue(value);
return ret;
};
};
Promise.attempt = Promise["try"] = function (fn, args, ctx) {
if (typeof fn !== "function") {
return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
}
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
ret._pushContext();
var value = util.isArray(args)
? tryCatch(fn).apply(ctx, args)
: tryCatch(fn).call(ctx, args);
ret._popContext();
ret._resolveFromSyncValue(value);
return ret;
};
Promise.prototype._resolveFromSyncValue = function (value) {
if (value === util.errorObj) {
this._rejectCallback(value.e, false, true);
} else {
this._resolveCallback(value, true);
}
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise) {
var util = require("./util.js");
var async = require("./async.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
function spreadAdapter(val, nodeback) {
var promise = this;
if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);
var ret =
tryCatch(nodeback).apply(promise._boundValue(), [null].concat(val));
if (ret === errorObj) {
async.throwLater(ret.e);
}
}
function successAdapter(val, nodeback) {
var promise = this;
var receiver = promise._boundValue();
var ret = val === undefined
? tryCatch(nodeback).call(receiver, null)
: tryCatch(nodeback).call(receiver, null, val);
if (ret === errorObj) {
async.throwLater(ret.e);
}
}
function errorAdapter(reason, nodeback) {
var promise = this;
if (!reason) {
var target = promise._target();
var newReason = target._getCarriedStackTrace();
newReason.cause = reason;
reason = newReason;
}
var ret = tryCatch(nodeback).call(promise._boundValue(), reason);
if (ret === errorObj) {
async.throwLater(ret.e);
}
}
Promise.prototype.asCallback =
Promise.prototype.nodeify = function (nodeback, options) {
if (typeof nodeback == "function") {
var adapter = successAdapter;
if (options !== undefined && Object(options).spread) {
adapter = spreadAdapter;
}
this._then(
adapter,
errorAdapter,
undefined,
this,
nodeback
);
}
return this;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, PromiseArray) {
var util = require("./util.js");
var async = require("./async.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
Promise.prototype.progressed = function (handler) {
return this._then(undefined, undefined, handler, undefined, undefined);
};
Promise.prototype._progress = function (progressValue) {
if (this._isFollowingOrFulfilledOrRejected()) return;
this._target()._progressUnchecked(progressValue);
};
Promise.prototype._progressHandlerAt = function (index) {
return index === 0
? this._progressHandler0
: this[(index << 2) + index - 5 + 2];
};
Promise.prototype._doProgressWith = function (progression) {
var progressValue = progression.value;
var handler = progression.handler;
var promise = progression.promise;
var receiver = progression.receiver;
var ret = tryCatch(handler).call(receiver, progressValue);
if (ret === errorObj) {
if (ret.e != null &&
ret.e.name !== "StopProgressPropagation") {
var trace = util.canAttachTrace(ret.e)
? ret.e : new Error(util.toString(ret.e));
promise._attachExtraTrace(trace);
promise._progress(ret.e);
}
} else if (ret instanceof Promise) {
ret._then(promise._progress, null, null, promise, undefined);
} else {
promise._progress(ret);
}
};
Promise.prototype._progressUnchecked = function (progressValue) {
var len = this._length();
var progress = this._progress;
for (var i = 0; i < len; i++) {
var handler = this._progressHandlerAt(i);
var promise = this._promiseAt(i);
if (!(promise instanceof Promise)) {
var receiver = this._receiverAt(i);
if (typeof handler === "function") {
handler.call(receiver, progressValue, promise);
} else if (receiver instanceof PromiseArray &&
!receiver._isResolved()) {
receiver._promiseProgressed(progressValue, promise);
}
continue;
}
if (typeof handler === "function") {
async.invoke(this._doProgressWith, this, {
handler: handler,
promise: promise,
receiver: this._receiverAt(i),
value: progressValue
});
} else {
async.invoke(progress, promise, progressValue);
}
}
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 9 9 9 9 9 9 9 9 9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 8 8 8 8 8 8 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function() {
var makeSelfResolutionError = function () {
return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/LhFpo0\u000a");
};
var reflect = function() {
return new Promise.PromiseInspection(this._target());
};
var apiRejection = function(msg) {
return Promise.reject(new TypeError(msg));
};
var util = require("./util.js");
var getDomain;
Eif (util.isNode) {
getDomain = function() {
var ret = process.domain;
if (ret === undefined) ret = null;
return ret;
};
} else {
getDomain = function() {
return null;
};
}
util.notEnumerableProp(Promise, "_getDomain", getDomain);
var UNDEFINED_BINDING = {};
var async = require("./async.js");
var errors = require("./errors.js");
var TypeError = Promise.TypeError = errors.TypeError;
Promise.RangeError = errors.RangeError;
Promise.CancellationError = errors.CancellationError;
Promise.TimeoutError = errors.TimeoutError;
Promise.OperationalError = errors.OperationalError;
Promise.RejectionError = errors.OperationalError;
Promise.AggregateError = errors.AggregateError;
var INTERNAL = function(){};
var APPLY = {};
var NEXT_FILTER = {e: null};
var tryConvertToPromise = require("./thenables.js")(Promise, INTERNAL);
var PromiseArray =
require("./promise_array.js")(Promise, INTERNAL,
tryConvertToPromise, apiRejection);
var CapturedTrace = require("./captured_trace.js")();
var isDebugging = require("./debuggability.js")(Promise, CapturedTrace);
/*jshint unused:false*/
var createContext =
require("./context.js")(Promise, CapturedTrace, isDebugging);
var CatchFilter = require("./catch_filter.js")(NEXT_FILTER);
var PromiseResolver = require("./promise_resolver.js");
var nodebackForPromise = PromiseResolver._nodebackForPromise;
var errorObj = util.errorObj;
var tryCatch = util.tryCatch;
function Promise(resolver) {
Iif (typeof resolver !== "function") {
throw new TypeError("the promise constructor requires a resolver function\u000a\u000a See http://goo.gl/EC22Yn\u000a");
}
Iif (this.constructor !== Promise) {
throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/KsIlge\u000a");
}
this._bitField = 0;
this._fulfillmentHandler0 = undefined;
this._rejectionHandler0 = undefined;
this._progressHandler0 = undefined;
this._promise0 = undefined;
this._receiver0 = undefined;
this._settledValue = undefined;
Iif (resolver !== INTERNAL) this._resolveFromResolver(resolver);
}
Promise.prototype.toString = function () {
return "[object Promise]";
};
Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
var len = arguments.length;
if (len > 1) {
var catchInstances = new Array(len - 1),
j = 0, i;
for (i = 0; i < len - 1; ++i) {
var item = arguments[i];
if (typeof item === "function") {
catchInstances[j++] = item;
} else {
return Promise.reject(
new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a"));
}
}
catchInstances.length = j;
fn = arguments[i];
var catchFilter = new CatchFilter(catchInstances, fn, this);
return this._then(undefined, catchFilter.doFilter, undefined,
catchFilter, undefined);
}
return this._then(undefined, fn, undefined, undefined, undefined);
};
Promise.prototype.reflect = function () {
return this._then(reflect, reflect, undefined, this, undefined);
};
Promise.prototype.then = function (didFulfill, didReject, didProgress) {
if (isDebugging() && arguments.length > 0 &&
typeof didFulfill !== "function" &&
typeof didReject !== "function") {
var msg = ".then() only accepts functions but was passed: " +
util.classString(didFulfill);
if (arguments.length > 1) {
msg += ", " + util.classString(didReject);
}
this._warn(msg);
}
return this._then(didFulfill, didReject, didProgress,
undefined, undefined);
};
Promise.prototype.done = function (didFulfill, didReject, didProgress) {
var promise = this._then(didFulfill, didReject, didProgress,
undefined, undefined);
promise._setIsFinal();
};
Promise.prototype.spread = function (didFulfill, didReject) {
return this.all()._then(didFulfill, didReject, undefined, APPLY, undefined);
};
Promise.prototype.isCancellable = function () {
return !this.isResolved() &&
this._cancellable();
};
Promise.prototype.toJSON = function () {
var ret = {
isFulfilled: false,
isRejected: false,
fulfillmentValue: undefined,
rejectionReason: undefined
};
if (this.isFulfilled()) {
ret.fulfillmentValue = this.value();
ret.isFulfilled = true;
} else if (this.isRejected()) {
ret.rejectionReason = this.reason();
ret.isRejected = true;
}
return ret;
};
Promise.prototype.all = function () {
return new PromiseArray(this).promise();
};
Promise.prototype.error = function (fn) {
return this.caught(util.originatesFromRejection, fn);
};
Promise.getNewLibraryCopy = module.exports;
Promise.is = function (val) {
return val instanceof Promise;
};
Promise.fromNode = function(fn) {
var ret = new Promise(INTERNAL);
var result = tryCatch(fn)(nodebackForPromise(ret));
if (result === errorObj) {
ret._rejectCallback(result.e, true, true);
}
return ret;
};
Promise.all = function (promises) {
return new PromiseArray(promises).promise();
};
Promise.defer = Promise.pending = function () {
var promise = new Promise(INTERNAL);
return new PromiseResolver(promise);
};
Promise.cast = function (obj) {
var ret = tryConvertToPromise(obj);
if (!(ret instanceof Promise)) {
var val = ret;
ret = new Promise(INTERNAL);
ret._fulfillUnchecked(val);
}
return ret;
};
Promise.resolve = Promise.fulfilled = Promise.cast;
Promise.reject = Promise.rejected = function (reason) {
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
ret._rejectCallback(reason, true);
return ret;
};
Promise.setScheduler = function(fn) {
if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
var prev = async._schedule;
async._schedule = fn;
return prev;
};
Promise.prototype._then = function (
didFulfill,
didReject,
didProgress,
receiver,
internalData
) {
var haveInternalData = internalData !== undefined;
var ret = haveInternalData ? internalData : new Promise(INTERNAL);
if (!haveInternalData) {
ret._propagateFrom(this, 4 | 1);
ret._captureStackTrace();
}
var target = this._target();
if (target !== this) {
if (receiver === undefined) receiver = this._boundTo;
if (!haveInternalData) ret._setIsMigrated();
}
var callbackIndex = target._addCallbacks(didFulfill,
didReject,
didProgress,
ret,
receiver,
getDomain());
if (target._isResolved() && !target._isSettlePromisesQueued()) {
async.invoke(
target._settlePromiseAtPostResolution, target, callbackIndex);
}
return ret;
};
Promise.prototype._settlePromiseAtPostResolution = function (index) {
if (this._isRejectionUnhandled()) this._unsetRejectionIsUnhandled();
this._settlePromiseAt(index);
};
Promise.prototype._length = function () {
return this._bitField & 131071;
};
Promise.prototype._isFollowingOrFulfilledOrRejected = function () {
return (this._bitField & 939524096) > 0;
};
Promise.prototype._isFollowing = function () {
return (this._bitField & 536870912) === 536870912;
};
Promise.prototype._setLength = function (len) {
this._bitField = (this._bitField & -131072) |
(len & 131071);
};
Promise.prototype._setFulfilled = function () {
this._bitField = this._bitField | 268435456;
};
Promise.prototype._setRejected = function () {
this._bitField = this._bitField | 134217728;
};
Promise.prototype._setFollowing = function () {
this._bitField = this._bitField | 536870912;
};
Promise.prototype._setIsFinal = function () {
this._bitField = this._bitField | 33554432;
};
Promise.prototype._isFinal = function () {
return (this._bitField & 33554432) > 0;
};
Promise.prototype._cancellable = function () {
return (this._bitField & 67108864) > 0;
};
Promise.prototype._setCancellable = function () {
this._bitField = this._bitField | 67108864;
};
Promise.prototype._unsetCancellable = function () {
this._bitField = this._bitField & (~67108864);
};
Promise.prototype._setIsMigrated = function () {
this._bitField = this._bitField | 4194304;
};
Promise.prototype._unsetIsMigrated = function () {
this._bitField = this._bitField & (~4194304);
};
Promise.prototype._isMigrated = function () {
return (this._bitField & 4194304) > 0;
};
Promise.prototype._receiverAt = function (index) {
var ret = index === 0
? this._receiver0
: this[
index * 5 - 5 + 4];
if (ret === UNDEFINED_BINDING) {
return undefined;
} else if (ret === undefined && this._isBound()) {
return this._boundValue();
}
return ret;
};
Promise.prototype._promiseAt = function (index) {
return index === 0
? this._promise0
: this[index * 5 - 5 + 3];
};
Promise.prototype._fulfillmentHandlerAt = function (index) {
return index === 0
? this._fulfillmentHandler0
: this[index * 5 - 5 + 0];
};
Promise.prototype._rejectionHandlerAt = function (index) {
return index === 0
? this._rejectionHandler0
: this[index * 5 - 5 + 1];
};
Promise.prototype._boundValue = function() {
var ret = this._boundTo;
if (ret !== undefined) {
if (ret instanceof Promise) {
if (ret.isFulfilled()) {
return ret.value();
} else {
return undefined;
}
}
}
return ret;
};
Promise.prototype._migrateCallbacks = function (follower, index) {
var fulfill = follower._fulfillmentHandlerAt(index);
var reject = follower._rejectionHandlerAt(index);
var progress = follower._progressHandlerAt(index);
var promise = follower._promiseAt(index);
var receiver = follower._receiverAt(index);
if (promise instanceof Promise) promise._setIsMigrated();
if (receiver === undefined) receiver = UNDEFINED_BINDING;
this._addCallbacks(fulfill, reject, progress, promise, receiver, null);
};
Promise.prototype._addCallbacks = function (
fulfill,
reject,
progress,
promise,
receiver,
domain
) {
var index = this._length();
if (index >= 131071 - 5) {
index = 0;
this._setLength(0);
}
if (index === 0) {
this._promise0 = promise;
if (receiver !== undefined) this._receiver0 = receiver;
if (typeof fulfill === "function" && !this._isCarryingStackTrace()) {
this._fulfillmentHandler0 =
domain === null ? fulfill : domain.bind(fulfill);
}
if (typeof reject === "function") {
this._rejectionHandler0 =
domain === null ? reject : domain.bind(reject);
}
if (typeof progress === "function") {
this._progressHandler0 =
domain === null ? progress : domain.bind(progress);
}
} else {
var base = index * 5 - 5;
this[base + 3] = promise;
this[base + 4] = receiver;
if (typeof fulfill === "function") {
this[base + 0] =
domain === null ? fulfill : domain.bind(fulfill);
}
if (typeof reject === "function") {
this[base + 1] =
domain === null ? reject : domain.bind(reject);
}
if (typeof progress === "function") {
this[base + 2] =
domain === null ? progress : domain.bind(progress);
}
}
this._setLength(index + 1);
return index;
};
Promise.prototype._setProxyHandlers = function (receiver, promiseSlotValue) {
var index = this._length();
if (index >= 131071 - 5) {
index = 0;
this._setLength(0);
}
if (index === 0) {
this._promise0 = promiseSlotValue;
this._receiver0 = receiver;
} else {
var base = index * 5 - 5;
this[base + 3] = promiseSlotValue;
this[base + 4] = receiver;
}
this._setLength(index + 1);
};
Promise.prototype._proxyPromiseArray = function (promiseArray, index) {
this._setProxyHandlers(promiseArray, index);
};
Promise.prototype._resolveCallback = function(value, shouldBind) {
if (this._isFollowingOrFulfilledOrRejected()) return;
if (value === this)
return this._rejectCallback(makeSelfResolutionError(), false, true);
var maybePromise = tryConvertToPromise(value, this);
if (!(maybePromise instanceof Promise)) return this._fulfill(value);
var propagationFlags = 1 | (shouldBind ? 4 : 0);
this._propagateFrom(maybePromise, propagationFlags);
var promise = maybePromise._target();
if (promise._isPending()) {
var len = this._length();
for (var i = 0; i < len; ++i) {
promise._migrateCallbacks(this, i);
}
this._setFollowing();
this._setLength(0);
this._setFollowee(promise);
} else if (promise._isFulfilled()) {
this._fulfillUnchecked(promise._value());
} else {
this._rejectUnchecked(promise._reason(),
promise._getCarriedStackTrace());
}
};
Promise.prototype._rejectCallback =
function(reason, synchronous, shouldNotMarkOriginatingFromRejection) {
if (!shouldNotMarkOriginatingFromRejection) {
util.markAsOriginatingFromRejection(reason);
}
var trace = util.ensureErrorObject(reason);
var hasStack = trace === reason;
this._attachExtraTrace(trace, synchronous ? hasStack : false);
this._reject(reason, hasStack ? undefined : trace);
};
Promise.prototype._resolveFromResolver = function (resolver) {
var promise = this;
this._captureStackTrace();
this._pushContext();
var synchronous = true;
var r = tryCatch(resolver)(function(value) {
if (promise === null) return;
promise._resolveCallback(value);
promise = null;
}, function (reason) {
if (promise === null) return;
promise._rejectCallback(reason, synchronous);
promise = null;
});
synchronous = false;
this._popContext();
if (r !== undefined && r === errorObj && promise !== null) {
promise._rejectCallback(r.e, true, true);
promise = null;
}
};
Promise.prototype._settlePromiseFromHandler = function (
handler, receiver, value, promise
) {
if (promise._isRejected()) return;
promise._pushContext();
var x;
if (receiver === APPLY && !this._isRejected()) {
x = tryCatch(handler).apply(this._boundValue(), value);
} else {
x = tryCatch(handler).call(receiver, value);
}
promise._popContext();
if (x === errorObj || x === promise || x === NEXT_FILTER) {
var err = x === promise ? makeSelfResolutionError() : x.e;
promise._rejectCallback(err, false, true);
} else {
promise._resolveCallback(x);
}
};
Promise.prototype._target = function() {
var ret = this;
while (ret._isFollowing()) ret = ret._followee();
return ret;
};
Promise.prototype._followee = function() {
return this._rejectionHandler0;
};
Promise.prototype._setFollowee = function(promise) {
this._rejectionHandler0 = promise;
};
Promise.prototype._cleanValues = function () {
if (this._cancellable()) {
this._cancellationParent = undefined;
}
};
Promise.prototype._propagateFrom = function (parent, flags) {
if ((flags & 1) > 0 && parent._cancellable()) {
this._setCancellable();
this._cancellationParent = parent;
}
if ((flags & 4) > 0 && parent._isBound()) {
this._setBoundTo(parent._boundTo);
}
};
Promise.prototype._fulfill = function (value) {
if (this._isFollowingOrFulfilledOrRejected()) return;
this._fulfillUnchecked(value);
};
Promise.prototype._reject = function (reason, carriedStackTrace) {
if (this._isFollowingOrFulfilledOrRejected()) return;
this._rejectUnchecked(reason, carriedStackTrace);
};
Promise.prototype._settlePromiseAt = function (index) {
var promise = this._promiseAt(index);
var isPromise = promise instanceof Promise;
if (isPromise && promise._isMigrated()) {
promise._unsetIsMigrated();
return async.invoke(this._settlePromiseAt, this, index);
}
var handler = this._isFulfilled()
? this._fulfillmentHandlerAt(index)
: this._rejectionHandlerAt(index);
var carriedStackTrace =
this._isCarryingStackTrace() ? this._getCarriedStackTrace() : undefined;
var value = this._settledValue;
var receiver = this._receiverAt(index);
this._clearCallbackDataAtIndex(index);
if (typeof handler === "function") {
if (!isPromise) {
handler.call(receiver, value, promise);
} else {
this._settlePromiseFromHandler(handler, receiver, value, promise);
}
} else if (receiver instanceof PromiseArray) {
if (!receiver._isResolved()) {
if (this._isFulfilled()) {
receiver._promiseFulfilled(value, promise);
}
else {
receiver._promiseRejected(value, promise);
}
}
} else if (isPromise) {
if (this._isFulfilled()) {
promise._fulfill(value);
} else {
promise._reject(value, carriedStackTrace);
}
}
if (index >= 4 && (index & 31) === 4)
async.invokeLater(this._setLength, this, 0);
};
Promise.prototype._clearCallbackDataAtIndex = function(index) {
if (index === 0) {
if (!this._isCarryingStackTrace()) {
this._fulfillmentHandler0 = undefined;
}
this._rejectionHandler0 =
this._progressHandler0 =
this._receiver0 =
this._promise0 = undefined;
} else {
var base = index * 5 - 5;
this[base + 3] =
this[base + 4] =
this[base + 0] =
this[base + 1] =
this[base + 2] = undefined;
}
};
Promise.prototype._isSettlePromisesQueued = function () {
return (this._bitField &
-1073741824) === -1073741824;
};
Promise.prototype._setSettlePromisesQueued = function () {
this._bitField = this._bitField | -1073741824;
};
Promise.prototype._unsetSettlePromisesQueued = function () {
this._bitField = this._bitField & (~-1073741824);
};
Promise.prototype._queueSettlePromises = function() {
async.settlePromises(this);
this._setSettlePromisesQueued();
};
Promise.prototype._fulfillUnchecked = function (value) {
if (value === this) {
var err = makeSelfResolutionError();
this._attachExtraTrace(err);
return this._rejectUnchecked(err, undefined);
}
this._setFulfilled();
this._settledValue = value;
this._cleanValues();
if (this._length() > 0) {
this._queueSettlePromises();
}
};
Promise.prototype._rejectUncheckedCheckError = function (reason) {
var trace = util.ensureErrorObject(reason);
this._rejectUnchecked(reason, trace === reason ? undefined : trace);
};
Promise.prototype._rejectUnchecked = function (reason, trace) {
if (reason === this) {
var err = makeSelfResolutionError();
this._attachExtraTrace(err);
return this._rejectUnchecked(err);
}
this._setRejected();
this._settledValue = reason;
this._cleanValues();
if (this._isFinal()) {
async.throwLater(function(e) {
if ("stack" in e) {
async.invokeFirst(
CapturedTrace.unhandledRejection, undefined, e);
}
throw e;
}, trace === undefined ? reason : trace);
return;
}
if (trace !== undefined && trace !== reason) {
this._setCarriedStackTrace(trace);
}
if (this._length() > 0) {
this._queueSettlePromises();
} else {
this._ensurePossibleRejectionHandled();
}
};
Promise.prototype._settlePromises = function () {
this._unsetSettlePromisesQueued();
var len = this._length();
for (var i = 0; i < len; i++) {
this._settlePromiseAt(i);
}
};
util.notEnumerableProp(Promise,
"_makeSelfResolutionError",
makeSelfResolutionError);
require("./progress.js")(Promise, PromiseArray);
require("./method.js")(Promise, INTERNAL, tryConvertToPromise, apiRejection);
require("./bind.js")(Promise, INTERNAL, tryConvertToPromise);
require("./finally.js")(Promise, NEXT_FILTER, tryConvertToPromise);
require("./direct_resolve.js")(Promise);
require("./synchronous_inspection.js")(Promise);
require("./join.js")(Promise, PromiseArray, tryConvertToPromise, INTERNAL);
Promise.version = "2.11.0";
Promise.Promise = Promise;
require('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
require('./cancel.js')(Promise);
require('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext);
require('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise);
require('./nodeify.js')(Promise);
require('./call_get.js')(Promise);
require('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
require('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
require('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
require('./settle.js')(Promise, PromiseArray);
require('./some.js')(Promise, PromiseArray, apiRejection);
require('./promisify.js')(Promise, INTERNAL);
require('./any.js')(Promise);
require('./each.js')(Promise, INTERNAL);
require('./timers.js')(Promise, INTERNAL);
require('./filter.js')(Promise, INTERNAL);
util.toFastProperties(Promise);
util.toFastProperties(Promise.prototype);
function fillTypes(value) {
var p = new Promise(INTERNAL);
p._fulfillmentHandler0 = value;
p._rejectionHandler0 = value;
p._progressHandler0 = value;
p._promise0 = value;
p._receiver0 = value;
p._settledValue = value;
}
// Complete slack tracking, opt out of field-type tracking and
// stabilize map
fillTypes({a: 1});
fillTypes({b: 2});
fillTypes({c: 3});
fillTypes(1);
fillTypes(function(){});
fillTypes(undefined);
fillTypes(false);
fillTypes(new Promise(INTERNAL));
CapturedTrace.setBounds(async.firstLineError, util.lastLineError);
return Promise;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL, tryConvertToPromise,
apiRejection) {
var util = require("./util.js");
var isArray = util.isArray;
function toResolutionValue(val) {
switch(val) {
case -2: return [];
case -3: return {};
}
}
function PromiseArray(values) {
var promise = this._promise = new Promise(INTERNAL);
var parent;
if (values instanceof Promise) {
parent = values;
promise._propagateFrom(parent, 1 | 4);
}
this._values = values;
this._length = 0;
this._totalResolved = 0;
this._init(undefined, -2);
}
PromiseArray.prototype.length = function () {
return this._length;
};
PromiseArray.prototype.promise = function () {
return this._promise;
};
PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
var values = tryConvertToPromise(this._values, this._promise);
if (values instanceof Promise) {
values = values._target();
this._values = values;
if (values._isFulfilled()) {
values = values._value();
if (!isArray(values)) {
var err = new Promise.TypeError("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");
this.__hardReject__(err);
return;
}
} else if (values._isPending()) {
values._then(
init,
this._reject,
undefined,
this,
resolveValueIfEmpty
);
return;
} else {
this._reject(values._reason());
return;
}
} else if (!isArray(values)) {
this._promise._reject(apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a")._reason());
return;
}
if (values.length === 0) {
if (resolveValueIfEmpty === -5) {
this._resolveEmptyArray();
}
else {
this._resolve(toResolutionValue(resolveValueIfEmpty));
}
return;
}
var len = this.getActualLength(values.length);
this._length = len;
this._values = this.shouldCopyValues() ? new Array(len) : this._values;
var promise = this._promise;
for (var i = 0; i < len; ++i) {
var isResolved = this._isResolved();
var maybePromise = tryConvertToPromise(values[i], promise);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
if (isResolved) {
maybePromise._ignoreRejections();
} else if (maybePromise._isPending()) {
maybePromise._proxyPromiseArray(this, i);
} else if (maybePromise._isFulfilled()) {
this._promiseFulfilled(maybePromise._value(), i);
} else {
this._promiseRejected(maybePromise._reason(), i);
}
} else if (!isResolved) {
this._promiseFulfilled(maybePromise, i);
}
}
};
PromiseArray.prototype._isResolved = function () {
return this._values === null;
};
PromiseArray.prototype._resolve = function (value) {
this._values = null;
this._promise._fulfill(value);
};
PromiseArray.prototype.__hardReject__ =
PromiseArray.prototype._reject = function (reason) {
this._values = null;
this._promise._rejectCallback(reason, false, true);
};
PromiseArray.prototype._promiseProgressed = function (progressValue, index) {
this._promise._progress({
index: index,
value: progressValue
});
};
PromiseArray.prototype._promiseFulfilled = function (value, index) {
this._values[index] = value;
var totalResolved = ++this._totalResolved;
if (totalResolved >= this._length) {
this._resolve(this._values);
}
};
PromiseArray.prototype._promiseRejected = function (reason, index) {
this._totalResolved++;
this._reject(reason);
};
PromiseArray.prototype.shouldCopyValues = function () {
return true;
};
PromiseArray.prototype.getActualLength = function (len) {
return len;
};
return PromiseArray;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
var util = require("./util.js");
var maybeWrapAsError = util.maybeWrapAsError;
var errors = require("./errors.js");
var TimeoutError = errors.TimeoutError;
var OperationalError = errors.OperationalError;
var haveGetters = util.haveGetters;
var es5 = require("./es5.js");
function isUntypedError(obj) {
return obj instanceof Error &&
es5.getPrototypeOf(obj) === Error.prototype;
}
var rErrorKey = /^(?:name|message|stack|cause)$/;
function wrapAsOperationalError(obj) {
var ret;
if (isUntypedError(obj)) {
ret = new OperationalError(obj);
ret.name = obj.name;
ret.message = obj.message;
ret.stack = obj.stack;
var keys = es5.keys(obj);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (!rErrorKey.test(key)) {
ret[key] = obj[key];
}
}
return ret;
}
util.markAsOriginatingFromRejection(obj);
return obj;
}
function nodebackForPromise(promise) {
return function(err, value) {
if (promise === null) return;
if (err) {
var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
promise._attachExtraTrace(wrapped);
promise._reject(wrapped);
} else if (arguments.length > 2) {
var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}
promise._fulfill(args);
} else {
promise._fulfill(value);
}
promise = null;
};
}
var PromiseResolver;
Iif (!haveGetters) {
PromiseResolver = function (promise) {
this.promise = promise;
this.asCallback = nodebackForPromise(promise);
this.callback = this.asCallback;
};
}
else {
PromiseResolver = function (promise) {
this.promise = promise;
};
}
Eif (haveGetters) {
var prop = {
get: function() {
return nodebackForPromise(this.promise);
}
};
es5.defineProperty(PromiseResolver.prototype, "asCallback", prop);
es5.defineProperty(PromiseResolver.prototype, "callback", prop);
}
PromiseResolver._nodebackForPromise = nodebackForPromise;
PromiseResolver.prototype.toString = function () {
return "[object PromiseResolver]";
};
PromiseResolver.prototype.resolve =
PromiseResolver.prototype.fulfill = function (value) {
if (!(this instanceof PromiseResolver)) {
throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
}
this.promise._resolveCallback(value);
};
PromiseResolver.prototype.reject = function (reason) {
if (!(this instanceof PromiseResolver)) {
throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
}
this.promise._rejectCallback(reason);
};
PromiseResolver.prototype.progress = function (value) {
if (!(this instanceof PromiseResolver)) {
throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
}
this.promise._progress(value);
};
PromiseResolver.prototype.cancel = function (err) {
this.promise.cancel(err);
};
PromiseResolver.prototype.timeout = function () {
this.reject(new TimeoutError("timeout"));
};
PromiseResolver.prototype.isResolved = function () {
return this.promise.isResolved();
};
PromiseResolver.prototype.toJSON = function () {
return this.promise.toJSON();
};
module.exports = PromiseResolver;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL) {
var THIS = {};
var util = require("./util.js");
var nodebackForPromise = require("./promise_resolver.js")
._nodebackForPromise;
var withAppended = util.withAppended;
var maybeWrapAsError = util.maybeWrapAsError;
var canEvaluate = util.canEvaluate;
var TypeError = require("./errors").TypeError;
var defaultSuffix = "Async";
var defaultPromisified = {__isPromisified__: true};
var noCopyProps = [
"arity", "length",
"name",
"arguments",
"caller",
"callee",
"prototype",
"__isPromisified__"
];
var noCopyPropsPattern = new RegExp("^(?:" + noCopyProps.join("|") + ")$");
var defaultFilter = function(name) {
return util.isIdentifier(name) &&
name.charAt(0) !== "_" &&
name !== "constructor";
};
function propsFilter(key) {
return !noCopyPropsPattern.test(key);
}
function isPromisified(fn) {
try {
return fn.__isPromisified__ === true;
}
catch (e) {
return false;
}
}
function hasPromisified(obj, key, suffix) {
var val = util.getDataPropertyOrDefault(obj, key + suffix,
defaultPromisified);
return val ? isPromisified(val) : false;
}
function checkValid(ret, suffix, suffixRegexp) {
for (var i = 0; i < ret.length; i += 2) {
var key = ret[i];
if (suffixRegexp.test(key)) {
var keyWithoutAsyncSuffix = key.replace(suffixRegexp, "");
for (var j = 0; j < ret.length; j += 2) {
if (ret[j] === keyWithoutAsyncSuffix) {
throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/iWrZbw\u000a"
.replace("%s", suffix));
}
}
}
}
}
function promisifiableMethods(obj, suffix, suffixRegexp, filter) {
var keys = util.inheritedDataKeys(obj);
var ret = [];
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var value = obj[key];
var passesDefaultFilter = filter === defaultFilter
? true : defaultFilter(key, value, obj);
if (typeof value === "function" &&
!isPromisified(value) &&
!hasPromisified(obj, key, suffix) &&
filter(key, value, obj, passesDefaultFilter)) {
ret.push(key, value);
}
}
checkValid(ret, suffix, suffixRegexp);
return ret;
}
var escapeIdentRegex = function(str) {
return str.replace(/([$])/, "\\$");
};
var makeNodePromisifiedEval;
Eif (!false) {
var switchCaseArgumentOrder = function(likelyArgumentCount) {
var ret = [likelyArgumentCount];
var min = Math.max(0, likelyArgumentCount - 1 - 3);
for(var i = likelyArgumentCount - 1; i >= min; --i) {
ret.push(i);
}
for(var i = likelyArgumentCount + 1; i <= 3; ++i) {
ret.push(i);
}
return ret;
};
var argumentSequence = function(argumentCount) {
return util.filledRange(argumentCount, "_arg", "");
};
var parameterDeclaration = function(parameterCount) {
return util.filledRange(
Math.max(parameterCount, 3), "_arg", "");
};
var parameterCount = function(fn) {
if (typeof fn.length === "number") {
return Math.max(Math.min(fn.length, 1023 + 1), 0);
}
return 0;
};
makeNodePromisifiedEval =
function(callback, receiver, originalName, fn) {
var newParameterCount = Math.max(0, parameterCount(fn) - 1);
var argumentOrder = switchCaseArgumentOrder(newParameterCount);
var shouldProxyThis = typeof callback === "string" || receiver === THIS;
function generateCallForArgumentCount(count) {
var args = argumentSequence(count).join(", ");
var comma = count > 0 ? ", " : "";
var ret;
if (shouldProxyThis) {
ret = "ret = callback.call(this, {{args}}, nodeback); break;\n";
} else {
ret = receiver === undefined
? "ret = callback({{args}}, nodeback); break;\n"
: "ret = callback.call(receiver, {{args}}, nodeback); break;\n";
}
return ret.replace("{{args}}", args).replace(", ", comma);
}
function generateArgumentSwitchCase() {
var ret = "";
for (var i = 0; i < argumentOrder.length; ++i) {
ret += "case " + argumentOrder[i] +":" +
generateCallForArgumentCount(argumentOrder[i]);
}
ret += " \n\
default: \n\
var args = new Array(len + 1); \n\
var i = 0; \n\
for (var i = 0; i < len; ++i) { \n\
args[i] = arguments[i]; \n\
} \n\
args[i] = nodeback; \n\
[CodeForCall] \n\
break; \n\
".replace("[CodeForCall]", (shouldProxyThis
? "ret = callback.apply(this, args);\n"
: "ret = callback.apply(receiver, args);\n"));
return ret;
}
var getFunctionCode = typeof callback === "string"
? ("this != null ? this['"+callback+"'] : fn")
: "fn";
return new Function("Promise",
"fn",
"receiver",
"withAppended",
"maybeWrapAsError",
"nodebackForPromise",
"tryCatch",
"errorObj",
"notEnumerableProp",
"INTERNAL","'use strict'; \n\
var ret = function (Parameters) { \n\
'use strict'; \n\
var len = arguments.length; \n\
var promise = new Promise(INTERNAL); \n\
promise._captureStackTrace(); \n\
var nodeback = nodebackForPromise(promise); \n\
var ret; \n\
var callback = tryCatch([GetFunctionCode]); \n\
switch(len) { \n\
[CodeForSwitchCase] \n\
} \n\
if (ret === errorObj) { \n\
promise._rejectCallback(maybeWrapAsError(ret.e), true, true);\n\
} \n\
return promise; \n\
}; \n\
notEnumerableProp(ret, '__isPromisified__', true); \n\
return ret; \n\
"
.replace("Parameters", parameterDeclaration(newParameterCount))
.replace("[CodeForSwitchCase]", generateArgumentSwitchCase())
.replace("[GetFunctionCode]", getFunctionCode))(
Promise,
fn,
receiver,
withAppended,
maybeWrapAsError,
nodebackForPromise,
util.tryCatch,
util.errorObj,
util.notEnumerableProp,
INTERNAL
);
};
}
function makeNodePromisifiedClosure(callback, receiver, _, fn) {
var defaultThis = (function() {return this;})();
var method = callback;
if (typeof method === "string") {
callback = fn;
}
function promisified() {
var _receiver = receiver;
if (receiver === THIS) _receiver = this;
var promise = new Promise(INTERNAL);
promise._captureStackTrace();
var cb = typeof method === "string" && this !== defaultThis
? this[method] : callback;
var fn = nodebackForPromise(promise);
try {
cb.apply(_receiver, withAppended(arguments, fn));
} catch(e) {
promise._rejectCallback(maybeWrapAsError(e), true, true);
}
return promise;
}
util.notEnumerableProp(promisified, "__isPromisified__", true);
return promisified;
}
var makeNodePromisified = canEvaluate
? makeNodePromisifiedEval
: makeNodePromisifiedClosure;
function promisifyAll(obj, suffix, filter, promisifier) {
var suffixRegexp = new RegExp(escapeIdentRegex(suffix) + "$");
var methods =
promisifiableMethods(obj, suffix, suffixRegexp, filter);
for (var i = 0, len = methods.length; i < len; i+= 2) {
var key = methods[i];
var fn = methods[i+1];
var promisifiedKey = key + suffix;
if (promisifier === makeNodePromisified) {
obj[promisifiedKey] =
makeNodePromisified(key, THIS, key, fn, suffix);
} else {
var promisified = promisifier(fn, function() {
return makeNodePromisified(key, THIS, key, fn, suffix);
});
util.notEnumerableProp(promisified, "__isPromisified__", true);
obj[promisifiedKey] = promisified;
}
}
util.toFastProperties(obj);
return obj;
}
function promisify(callback, receiver) {
return makeNodePromisified(callback, receiver, undefined, callback);
}
Promise.promisify = function (fn, receiver) {
if (typeof fn !== "function") {
throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
}
if (isPromisified(fn)) {
return fn;
}
var ret = promisify(fn, arguments.length < 2 ? THIS : receiver);
util.copyDescriptors(fn, ret, propsFilter);
return ret;
};
Promise.promisifyAll = function (target, options) {
if (typeof target !== "function" && typeof target !== "object") {
throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/9ITlV0\u000a");
}
options = Object(options);
var suffix = options.suffix;
if (typeof suffix !== "string") suffix = defaultSuffix;
var filter = options.filter;
if (typeof filter !== "function") filter = defaultFilter;
var promisifier = options.promisifier;
if (typeof promisifier !== "function") promisifier = makeNodePromisified;
if (!util.isIdentifier(suffix)) {
throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/8FZo5V\u000a");
}
var keys = util.inheritedDataKeys(target);
for (var i = 0; i < keys.length; ++i) {
var value = target[keys[i]];
if (keys[i] !== "constructor" &&
util.isClass(value)) {
promisifyAll(value.prototype, suffix, filter, promisifier);
promisifyAll(value, suffix, filter, promisifier);
}
}
return promisifyAll(target, suffix, filter, promisifier);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(
Promise, PromiseArray, tryConvertToPromise, apiRejection) {
var util = require("./util.js");
var isObject = util.isObject;
var es5 = require("./es5.js");
function PropertiesPromiseArray(obj) {
var keys = es5.keys(obj);
var len = keys.length;
var values = new Array(len * 2);
for (var i = 0; i < len; ++i) {
var key = keys[i];
values[i] = obj[key];
values[i + len] = key;
}
this.constructor$(values);
}
util.inherits(PropertiesPromiseArray, PromiseArray);
PropertiesPromiseArray.prototype._init = function () {
this._init$(undefined, -3) ;
};
PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
this._values[index] = value;
var totalResolved = ++this._totalResolved;
if (totalResolved >= this._length) {
var val = {};
var keyOffset = this.length();
for (var i = 0, len = this.length(); i < len; ++i) {
val[this._values[i + keyOffset]] = this._values[i];
}
this._resolve(val);
}
};
PropertiesPromiseArray.prototype._promiseProgressed = function (value, index) {
this._promise._progress({
key: this._values[index + this.length()],
value: value
});
};
PropertiesPromiseArray.prototype.shouldCopyValues = function () {
return false;
};
PropertiesPromiseArray.prototype.getActualLength = function (len) {
return len >> 1;
};
function props(promises) {
var ret;
var castValue = tryConvertToPromise(promises);
if (!isObject(castValue)) {
return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/OsFKC8\u000a");
} else if (castValue instanceof Promise) {
ret = castValue._then(
Promise.props, undefined, undefined, undefined, undefined);
} else {
ret = new PropertiesPromiseArray(castValue).promise();
}
if (castValue instanceof Promise) {
ret._propagateFrom(castValue, 4);
}
return ret;
}
Promise.prototype.props = function () {
return props(this);
};
Promise.props = function (promises) {
return props(promises);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 | "use strict"; function arrayMove(src, srcIndex, dst, dstIndex, len) { for (var j = 0; j < len; ++j) { dst[j + dstIndex] = src[j + srcIndex]; src[j + srcIndex] = void 0; } } function Queue(capacity) { this._capacity = capacity; this._length = 0; this._front = 0; } Queue.prototype._willBeOverCapacity = function (size) { return this._capacity < size; }; Queue.prototype._pushOne = function (arg) { var length = this.length(); this._checkCapacity(length + 1); var i = (this._front + length) & (this._capacity - 1); this[i] = arg; this._length = length + 1; }; Queue.prototype._unshiftOne = function(value) { var capacity = this._capacity; this._checkCapacity(this.length() + 1); var front = this._front; var i = (((( front - 1 ) & ( capacity - 1) ) ^ capacity ) - capacity ); this[i] = value; this._front = i; this._length = this.length() + 1; }; Queue.prototype.unshift = function(fn, receiver, arg) { this._unshiftOne(arg); this._unshiftOne(receiver); this._unshiftOne(fn); }; Queue.prototype.push = function (fn, receiver, arg) { var length = this.length() + 3; if (this._willBeOverCapacity(length)) { this._pushOne(fn); this._pushOne(receiver); this._pushOne(arg); return; } var j = this._front + length - 3; this._checkCapacity(length); var wrapMask = this._capacity - 1; this[(j + 0) & wrapMask] = fn; this[(j + 1) & wrapMask] = receiver; this[(j + 2) & wrapMask] = arg; this._length = length; }; Queue.prototype.shift = function () { var front = this._front, ret = this[front]; this[front] = undefined; this._front = (front + 1) & (this._capacity - 1); this._length--; return ret; }; Queue.prototype.length = function () { return this._length; }; Queue.prototype._checkCapacity = function (size) { if (this._capacity < size) { this._resizeTo(this._capacity << 1); } }; Queue.prototype._resizeTo = function (capacity) { var oldCapacity = this._capacity; this._capacity = capacity; var front = this._front; var length = this._length; var moveItemsCount = (front + length) & (oldCapacity - 1); arrayMove(this, 0, this, oldCapacity, moveItemsCount); }; module.exports = Queue; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 1 1 1 1 1 1 | "use strict";
module.exports = function(
Promise, INTERNAL, tryConvertToPromise, apiRejection) {
var isArray = require("./util.js").isArray;
var raceLater = function (promise) {
return promise.then(function(array) {
return race(array, promise);
});
};
function race(promises, parent) {
var maybePromise = tryConvertToPromise(promises);
if (maybePromise instanceof Promise) {
return raceLater(maybePromise);
} else if (!isArray(promises)) {
return apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");
}
var ret = new Promise(INTERNAL);
if (parent !== undefined) {
ret._propagateFrom(parent, 4 | 1);
}
var fulfill = ret._fulfill;
var reject = ret._reject;
for (var i = 0, len = promises.length; i < len; ++i) {
var val = promises[i];
if (val === undefined && !(i in promises)) {
continue;
}
Promise.cast(val)._then(fulfill, reject, undefined, ret, null);
}
return ret;
}
Promise.race = function (promises) {
return race(promises, undefined);
};
Promise.prototype.race = function () {
return race(this, undefined);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise,
PromiseArray,
apiRejection,
tryConvertToPromise,
INTERNAL) {
var getDomain = Promise._getDomain;
var async = require("./async.js");
var util = require("./util.js");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
function ReductionPromiseArray(promises, fn, accum, _each) {
this.constructor$(promises);
this._promise._captureStackTrace();
this._preservedValues = _each === INTERNAL ? [] : null;
this._zerothIsAccum = (accum === undefined);
this._gotAccum = false;
this._reducingIndex = (this._zerothIsAccum ? 1 : 0);
this._valuesPhase = undefined;
var maybePromise = tryConvertToPromise(accum, this._promise);
var rejected = false;
var isPromise = maybePromise instanceof Promise;
if (isPromise) {
maybePromise = maybePromise._target();
if (maybePromise._isPending()) {
maybePromise._proxyPromiseArray(this, -1);
} else if (maybePromise._isFulfilled()) {
accum = maybePromise._value();
this._gotAccum = true;
} else {
this._reject(maybePromise._reason());
rejected = true;
}
}
if (!(isPromise || this._zerothIsAccum)) this._gotAccum = true;
var domain = getDomain();
this._callback = domain === null ? fn : domain.bind(fn);
this._accum = accum;
if (!rejected) async.invoke(init, this, undefined);
}
function init() {
this._init$(undefined, -5);
}
util.inherits(ReductionPromiseArray, PromiseArray);
ReductionPromiseArray.prototype._init = function () {};
ReductionPromiseArray.prototype._resolveEmptyArray = function () {
if (this._gotAccum || this._zerothIsAccum) {
this._resolve(this._preservedValues !== null
? [] : this._accum);
}
};
ReductionPromiseArray.prototype._promiseFulfilled = function (value, index) {
var values = this._values;
values[index] = value;
var length = this.length();
var preservedValues = this._preservedValues;
var isEach = preservedValues !== null;
var gotAccum = this._gotAccum;
var valuesPhase = this._valuesPhase;
var valuesPhaseIndex;
if (!valuesPhase) {
valuesPhase = this._valuesPhase = new Array(length);
for (valuesPhaseIndex=0; valuesPhaseIndex<length; ++valuesPhaseIndex) {
valuesPhase[valuesPhaseIndex] = 0;
}
}
valuesPhaseIndex = valuesPhase[index];
if (index === 0 && this._zerothIsAccum) {
this._accum = value;
this._gotAccum = gotAccum = true;
valuesPhase[index] = ((valuesPhaseIndex === 0)
? 1 : 2);
} else if (index === -1) {
this._accum = value;
this._gotAccum = gotAccum = true;
} else {
if (valuesPhaseIndex === 0) {
valuesPhase[index] = 1;
} else {
valuesPhase[index] = 2;
this._accum = value;
}
}
if (!gotAccum) return;
var callback = this._callback;
var receiver = this._promise._boundValue();
var ret;
for (var i = this._reducingIndex; i < length; ++i) {
valuesPhaseIndex = valuesPhase[i];
if (valuesPhaseIndex === 2) {
this._reducingIndex = i + 1;
continue;
}
if (valuesPhaseIndex !== 1) return;
value = values[i];
this._promise._pushContext();
if (isEach) {
preservedValues.push(value);
ret = tryCatch(callback).call(receiver, value, i, length);
}
else {
ret = tryCatch(callback)
.call(receiver, this._accum, value, i, length);
}
this._promise._popContext();
if (ret === errorObj) return this._reject(ret.e);
var maybePromise = tryConvertToPromise(ret, this._promise);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
if (maybePromise._isPending()) {
valuesPhase[i] = 4;
return maybePromise._proxyPromiseArray(this, i);
} else if (maybePromise._isFulfilled()) {
ret = maybePromise._value();
} else {
return this._reject(maybePromise._reason());
}
}
this._reducingIndex = i + 1;
this._accum = ret;
}
this._resolve(isEach ? preservedValues : this._accum);
};
function reduce(promises, fn, initialValue, _each) {
if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
var array = new ReductionPromiseArray(promises, fn, initialValue, _each);
return array.promise();
}
Promise.prototype.reduce = function (fn, initialValue) {
return reduce(this, fn, initialValue, null);
};
Promise.reduce = function (promises, fn, initialValue, _each) {
return reduce(promises, fn, initialValue, _each);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 1 1 1 1 1 1 1 | "use strict";
var schedule;
var util = require("./util");
var noAsyncScheduler = function() {
throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");
};
Eif (util.isNode && typeof MutationObserver === "undefined") {
var GlobalSetImmediate = global.setImmediate;
var ProcessNextTick = process.nextTick;
schedule = util.isRecentNode
? function(fn) { GlobalSetImmediate.call(global, fn); }
: function(fn) { ProcessNextTick.call(process, fn); };
} else if ((typeof MutationObserver !== "undefined") &&
!(typeof window !== "undefined" &&
window.navigator &&
window.navigator.standalone)) {
schedule = function(fn) {
var div = document.createElement("div");
var observer = new MutationObserver(fn);
observer.observe(div, {attributes: true});
return function() { div.classList.toggle("foo"); };
};
schedule.isStatic = true;
} else if (typeof setImmediate !== "undefined") {
schedule = function (fn) {
setImmediate(fn);
};
} else if (typeof setTimeout !== "undefined") {
schedule = function (fn) {
setTimeout(fn, 0);
};
} else {
schedule = noAsyncScheduler;
}
module.exports = schedule;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports =
function(Promise, PromiseArray) {
var PromiseInspection = Promise.PromiseInspection;
var util = require("./util.js");
function SettledPromiseArray(values) {
this.constructor$(values);
}
util.inherits(SettledPromiseArray, PromiseArray);
SettledPromiseArray.prototype._promiseResolved = function (index, inspection) {
this._values[index] = inspection;
var totalResolved = ++this._totalResolved;
if (totalResolved >= this._length) {
this._resolve(this._values);
}
};
SettledPromiseArray.prototype._promiseFulfilled = function (value, index) {
var ret = new PromiseInspection();
ret._bitField = 268435456;
ret._settledValue = value;
this._promiseResolved(index, ret);
};
SettledPromiseArray.prototype._promiseRejected = function (reason, index) {
var ret = new PromiseInspection();
ret._bitField = 134217728;
ret._settledValue = reason;
this._promiseResolved(index, ret);
};
Promise.settle = function (promises) {
return new SettledPromiseArray(promises).promise();
};
Promise.prototype.settle = function () {
return new SettledPromiseArray(this).promise();
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports =
function(Promise, PromiseArray, apiRejection) {
var util = require("./util.js");
var RangeError = require("./errors.js").RangeError;
var AggregateError = require("./errors.js").AggregateError;
var isArray = util.isArray;
function SomePromiseArray(values) {
this.constructor$(values);
this._howMany = 0;
this._unwrap = false;
this._initialized = false;
}
util.inherits(SomePromiseArray, PromiseArray);
SomePromiseArray.prototype._init = function () {
if (!this._initialized) {
return;
}
if (this._howMany === 0) {
this._resolve([]);
return;
}
this._init$(undefined, -5);
var isArrayResolved = isArray(this._values);
if (!this._isResolved() &&
isArrayResolved &&
this._howMany > this._canPossiblyFulfill()) {
this._reject(this._getRangeError(this.length()));
}
};
SomePromiseArray.prototype.init = function () {
this._initialized = true;
this._init();
};
SomePromiseArray.prototype.setUnwrap = function () {
this._unwrap = true;
};
SomePromiseArray.prototype.howMany = function () {
return this._howMany;
};
SomePromiseArray.prototype.setHowMany = function (count) {
this._howMany = count;
};
SomePromiseArray.prototype._promiseFulfilled = function (value) {
this._addFulfilled(value);
if (this._fulfilled() === this.howMany()) {
this._values.length = this.howMany();
if (this.howMany() === 1 && this._unwrap) {
this._resolve(this._values[0]);
} else {
this._resolve(this._values);
}
}
};
SomePromiseArray.prototype._promiseRejected = function (reason) {
this._addRejected(reason);
if (this.howMany() > this._canPossiblyFulfill()) {
var e = new AggregateError();
for (var i = this.length(); i < this._values.length; ++i) {
e.push(this._values[i]);
}
this._reject(e);
}
};
SomePromiseArray.prototype._fulfilled = function () {
return this._totalResolved;
};
SomePromiseArray.prototype._rejected = function () {
return this._values.length - this.length();
};
SomePromiseArray.prototype._addRejected = function (reason) {
this._values.push(reason);
};
SomePromiseArray.prototype._addFulfilled = function (value) {
this._values[this._totalResolved++] = value;
};
SomePromiseArray.prototype._canPossiblyFulfill = function () {
return this.length() - this._rejected();
};
SomePromiseArray.prototype._getRangeError = function (count) {
var message = "Input array must contain at least " +
this._howMany + " items but contains only " + count + " items";
return new RangeError(message);
};
SomePromiseArray.prototype._resolveEmptyArray = function () {
this._reject(this._getRangeError(0));
};
function some(promises, howMany) {
if ((howMany | 0) !== howMany || howMany < 0) {
return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/1wAmHx\u000a");
}
var ret = new SomePromiseArray(promises);
var promise = ret.promise();
ret.setHowMany(howMany);
ret.init();
return promise;
}
Promise.some = function (promises, howMany) {
return some(promises, howMany);
};
Promise.prototype.some = function (howMany) {
return some(this, howMany);
};
Promise._SomePromiseArray = SomePromiseArray;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise) {
function PromiseInspection(promise) {
if (promise !== undefined) {
promise = promise._target();
this._bitField = promise._bitField;
this._settledValue = promise._settledValue;
}
else {
this._bitField = 0;
this._settledValue = undefined;
}
}
PromiseInspection.prototype.value = function () {
if (!this.isFulfilled()) {
throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/hc1DLj\u000a");
}
return this._settledValue;
};
PromiseInspection.prototype.error =
PromiseInspection.prototype.reason = function () {
if (!this.isRejected()) {
throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/hPuiwB\u000a");
}
return this._settledValue;
};
PromiseInspection.prototype.isFulfilled =
Promise.prototype._isFulfilled = function () {
return (this._bitField & 268435456) > 0;
};
PromiseInspection.prototype.isRejected =
Promise.prototype._isRejected = function () {
return (this._bitField & 134217728) > 0;
};
PromiseInspection.prototype.isPending =
Promise.prototype._isPending = function () {
return (this._bitField & 402653184) === 0;
};
PromiseInspection.prototype.isResolved =
Promise.prototype._isResolved = function () {
return (this._bitField & 402653184) > 0;
};
Promise.prototype.isPending = function() {
return this._target()._isPending();
};
Promise.prototype.isRejected = function() {
return this._target()._isRejected();
};
Promise.prototype.isFulfilled = function() {
return this._target()._isFulfilled();
};
Promise.prototype.isResolved = function() {
return this._target()._isResolved();
};
Promise.prototype._value = function() {
return this._settledValue;
};
Promise.prototype._reason = function() {
this._unsetRejectionIsUnhandled();
return this._settledValue;
};
Promise.prototype.value = function() {
var target = this._target();
if (!target.isFulfilled()) {
throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/hc1DLj\u000a");
}
return target._settledValue;
};
Promise.prototype.reason = function() {
var target = this._target();
if (!target.isRejected()) {
throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/hPuiwB\u000a");
}
target._unsetRejectionIsUnhandled();
return target._settledValue;
};
Promise.PromiseInspection = PromiseInspection;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL) {
var util = require("./util.js");
var errorObj = util.errorObj;
var isObject = util.isObject;
function tryConvertToPromise(obj, context) {
if (isObject(obj)) {
if (obj instanceof Promise) {
return obj;
}
else if (isAnyBluebirdPromise(obj)) {
var ret = new Promise(INTERNAL);
obj._then(
ret._fulfillUnchecked,
ret._rejectUncheckedCheckError,
ret._progressUnchecked,
ret,
null
);
return ret;
}
var then = util.tryCatch(getThen)(obj);
if (then === errorObj) {
if (context) context._pushContext();
var ret = Promise.reject(then.e);
if (context) context._popContext();
return ret;
} else if (typeof then === "function") {
return doThenable(obj, then, context);
}
}
return obj;
}
function getThen(obj) {
return obj.then;
}
var hasProp = {}.hasOwnProperty;
function isAnyBluebirdPromise(obj) {
return hasProp.call(obj, "_promise0");
}
function doThenable(x, then, context) {
var promise = new Promise(INTERNAL);
var ret = promise;
if (context) context._pushContext();
promise._captureStackTrace();
if (context) context._popContext();
var synchronous = true;
var result = util.tryCatch(then).call(x,
resolveFromThenable,
rejectFromThenable,
progressFromThenable);
synchronous = false;
if (promise && result === errorObj) {
promise._rejectCallback(result.e, true, true);
promise = null;
}
function resolveFromThenable(value) {
if (!promise) return;
promise._resolveCallback(value);
promise = null;
}
function rejectFromThenable(reason) {
if (!promise) return;
promise._rejectCallback(reason, synchronous, true);
promise = null;
}
function progressFromThenable(value) {
if (!promise) return;
if (typeof promise._progress === "function") {
promise._progress(value);
}
}
return ret;
}
return tryConvertToPromise;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function(Promise, INTERNAL) {
var util = require("./util.js");
var TimeoutError = Promise.TimeoutError;
var afterTimeout = function (promise, message) {
if (!promise.isPending()) return;
var err;
if(!util.isPrimitive(message) && (message instanceof Error)) {
err = message;
} else {
if (typeof message !== "string") {
message = "operation timed out";
}
err = new TimeoutError(message);
}
util.markAsOriginatingFromRejection(err);
promise._attachExtraTrace(err);
promise._cancel(err);
};
var afterValue = function(value) { return delay(+this).thenReturn(value); };
var delay = Promise.delay = function (value, ms) {
if (ms === undefined) {
ms = value;
value = undefined;
var ret = new Promise(INTERNAL);
setTimeout(function() { ret._fulfill(); }, ms);
return ret;
}
ms = +ms;
return Promise.resolve(value)._then(afterValue, null, null, ms, undefined);
};
Promise.prototype.delay = function (ms) {
return delay(this, ms);
};
function successClear(value) {
var handle = this;
if (handle instanceof Number) handle = +handle;
clearTimeout(handle);
return value;
}
function failureClear(reason) {
var handle = this;
if (handle instanceof Number) handle = +handle;
clearTimeout(handle);
throw reason;
}
Promise.prototype.timeout = function (ms, message) {
ms = +ms;
var ret = this.then().cancellable();
ret._cancellationParent = this;
var handle = setTimeout(function timeoutTimeout() {
afterTimeout(ret, message);
}, ms);
return ret._then(successClear, failureClear, undefined, handle, undefined);
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
module.exports = function (Promise, apiRejection, tryConvertToPromise,
createContext) {
var TypeError = require("./errors.js").TypeError;
var inherits = require("./util.js").inherits;
var PromiseInspection = Promise.PromiseInspection;
function inspectionMapper(inspections) {
var len = inspections.length;
for (var i = 0; i < len; ++i) {
var inspection = inspections[i];
if (inspection.isRejected()) {
return Promise.reject(inspection.error());
}
inspections[i] = inspection._settledValue;
}
return inspections;
}
function thrower(e) {
setTimeout(function(){throw e;}, 0);
}
function castPreservingDisposable(thenable) {
var maybePromise = tryConvertToPromise(thenable);
if (maybePromise !== thenable &&
typeof thenable._isDisposable === "function" &&
typeof thenable._getDisposer === "function" &&
thenable._isDisposable()) {
maybePromise._setDisposable(thenable._getDisposer());
}
return maybePromise;
}
function dispose(resources, inspection) {
var i = 0;
var len = resources.length;
var ret = Promise.defer();
function iterator() {
if (i >= len) return ret.resolve();
var maybePromise = castPreservingDisposable(resources[i++]);
if (maybePromise instanceof Promise &&
maybePromise._isDisposable()) {
try {
maybePromise = tryConvertToPromise(
maybePromise._getDisposer().tryDispose(inspection),
resources.promise);
} catch (e) {
return thrower(e);
}
if (maybePromise instanceof Promise) {
return maybePromise._then(iterator, thrower,
null, null, null);
}
}
iterator();
}
iterator();
return ret.promise;
}
function disposerSuccess(value) {
var inspection = new PromiseInspection();
inspection._settledValue = value;
inspection._bitField = 268435456;
return dispose(this, inspection).thenReturn(value);
}
function disposerFail(reason) {
var inspection = new PromiseInspection();
inspection._settledValue = reason;
inspection._bitField = 134217728;
return dispose(this, inspection).thenThrow(reason);
}
function Disposer(data, promise, context) {
this._data = data;
this._promise = promise;
this._context = context;
}
Disposer.prototype.data = function () {
return this._data;
};
Disposer.prototype.promise = function () {
return this._promise;
};
Disposer.prototype.resource = function () {
if (this.promise().isFulfilled()) {
return this.promise().value();
}
return null;
};
Disposer.prototype.tryDispose = function(inspection) {
var resource = this.resource();
var context = this._context;
if (context !== undefined) context._pushContext();
var ret = resource !== null
? this.doDispose(resource, inspection) : null;
if (context !== undefined) context._popContext();
this._promise._unsetDisposable();
this._data = null;
return ret;
};
Disposer.isDisposer = function (d) {
return (d != null &&
typeof d.resource === "function" &&
typeof d.tryDispose === "function");
};
function FunctionDisposer(fn, promise, context) {
this.constructor$(fn, promise, context);
}
inherits(FunctionDisposer, Disposer);
FunctionDisposer.prototype.doDispose = function (resource, inspection) {
var fn = this.data();
return fn.call(resource, resource, inspection);
};
function maybeUnwrapDisposer(value) {
if (Disposer.isDisposer(value)) {
this.resources[this.index]._setDisposable(value);
return value.promise();
}
return value;
}
Promise.using = function () {
var len = arguments.length;
if (len < 2) return apiRejection(
"you must pass at least 2 arguments to Promise.using");
var fn = arguments[len - 1];
if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
var input;
var spreadArgs = true;
if (len === 2 && Array.isArray(arguments[0])) {
input = arguments[0];
len = input.length;
spreadArgs = false;
} else {
input = arguments;
len--;
}
var resources = new Array(len);
for (var i = 0; i < len; ++i) {
var resource = input[i];
if (Disposer.isDisposer(resource)) {
var disposer = resource;
resource = resource.promise();
resource._setDisposable(disposer);
} else {
var maybePromise = tryConvertToPromise(resource);
if (maybePromise instanceof Promise) {
resource =
maybePromise._then(maybeUnwrapDisposer, null, null, {
resources: resources,
index: i
}, undefined);
}
}
resources[i] = resource;
}
var promise = Promise.settle(resources)
.then(inspectionMapper)
.then(function(vals) {
promise._pushContext();
var ret;
try {
ret = spreadArgs
? fn.apply(undefined, vals) : fn.call(undefined, vals);
} finally {
promise._popContext();
}
return ret;
})
._then(
disposerSuccess, disposerFail, undefined, resources, undefined);
resources.promise = promise;
return promise;
};
Promise.prototype._setDisposable = function (disposer) {
this._bitField = this._bitField | 262144;
this._disposer = disposer;
};
Promise.prototype._isDisposable = function () {
return (this._bitField & 262144) > 0;
};
Promise.prototype._getDisposer = function () {
return this._disposer;
};
Promise.prototype._unsetDisposable = function () {
this._bitField = this._bitField & (~262144);
this._disposer = undefined;
};
Promise.prototype.disposer = function (fn) {
if (typeof fn === "function") {
return new FunctionDisposer(fn, this, createContext());
}
throw new TypeError();
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | 1 1 1 1 1 1 1 1 1 1 1 1 1 12 1 12 12 12 64 64 12 12 12 1 2 1 1 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 3 3 24 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | "use strict";
var es5 = require("./es5.js");
var canEvaluate = typeof navigator == "undefined";
var haveGetters = (function(){
try {
var o = {};
es5.defineProperty(o, "f", {
get: function () {
return 3;
}
});
return o.f === 3;
}
catch (e) {
return false;
}
})();
var errorObj = {e: {}};
var tryCatchTarget;
function tryCatcher() {
try {
var target = tryCatchTarget;
tryCatchTarget = null;
return target.apply(this, arguments);
} catch (e) {
errorObj.e = e;
return errorObj;
}
}
function tryCatch(fn) {
tryCatchTarget = fn;
return tryCatcher;
}
var inherits = function(Child, Parent) {
var hasProp = {}.hasOwnProperty;
function T() {
this.constructor = Child;
this.constructor$ = Parent;
for (var propertyName in Parent.prototype) {
Eif (hasProp.call(Parent.prototype, propertyName) &&
propertyName.charAt(propertyName.length-1) !== "$"
) {
this[propertyName + "$"] = Parent.prototype[propertyName];
}
}
}
T.prototype = Parent.prototype;
Child.prototype = new T();
return Child.prototype;
};
function isPrimitive(val) {
return val == null || val === true || val === false ||
typeof val === "string" || typeof val === "number";
}
function isObject(value) {
return !isPrimitive(value);
}
function maybeWrapAsError(maybeError) {
if (!isPrimitive(maybeError)) return maybeError;
return new Error(safeToString(maybeError));
}
function withAppended(target, appendee) {
var len = target.length;
var ret = new Array(len + 1);
var i;
for (i = 0; i < len; ++i) {
ret[i] = target[i];
}
ret[i] = appendee;
return ret;
}
function getDataPropertyOrDefault(obj, key, defaultValue) {
if (es5.isES5) {
var desc = Object.getOwnPropertyDescriptor(obj, key);
if (desc != null) {
return desc.get == null && desc.set == null
? desc.value
: defaultValue;
}
} else {
return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
}
}
function notEnumerableProp(obj, name, value) {
Iif (isPrimitive(obj)) return obj;
var descriptor = {
value: value,
configurable: true,
enumerable: false,
writable: true
};
es5.defineProperty(obj, name, descriptor);
return obj;
}
function thrower(r) {
throw r;
}
var inheritedDataKeys = (function() {
var excludedPrototypes = [
Array.prototype,
Object.prototype,
Function.prototype
];
var isExcludedProto = function(val) {
for (var i = 0; i < excludedPrototypes.length; ++i) {
if (excludedPrototypes[i] === val) {
return true;
}
}
return false;
};
Eif (es5.isES5) {
var getKeys = Object.getOwnPropertyNames;
return function(obj) {
var ret = [];
var visitedKeys = Object.create(null);
while (obj != null && !isExcludedProto(obj)) {
var keys;
try {
keys = getKeys(obj);
} catch (e) {
return ret;
}
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (visitedKeys[key]) continue;
visitedKeys[key] = true;
var desc = Object.getOwnPropertyDescriptor(obj, key);
if (desc != null && desc.get == null && desc.set == null) {
ret.push(key);
}
}
obj = es5.getPrototypeOf(obj);
}
return ret;
};
} else {
var hasProp = {}.hasOwnProperty;
return function(obj) {
if (isExcludedProto(obj)) return [];
var ret = [];
/*jshint forin:false */
enumeration: for (var key in obj) {
if (hasProp.call(obj, key)) {
ret.push(key);
} else {
for (var i = 0; i < excludedPrototypes.length; ++i) {
if (hasProp.call(excludedPrototypes[i], key)) {
continue enumeration;
}
}
ret.push(key);
}
}
return ret;
};
}
})();
var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
function isClass(fn) {
try {
if (typeof fn === "function") {
var keys = es5.names(fn.prototype);
var hasMethods = es5.isES5 && keys.length > 1;
var hasMethodsOtherThanConstructor = keys.length > 0 &&
!(keys.length === 1 && keys[0] === "constructor");
var hasThisAssignmentAndStaticMethods =
thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
if (hasMethods || hasMethodsOtherThanConstructor ||
hasThisAssignmentAndStaticMethods) {
return true;
}
}
return false;
} catch (e) {
return false;
}
}
function toFastProperties(obj) {
/*jshint -W027,-W055,-W031*/
function f() {}
f.prototype = obj;
var l = 8;
while (l--) new f();
return obj;
eval(obj);
}
var rident = /^[a-z$_][a-z$_0-9]*$/i;
function isIdentifier(str) {
return rident.test(str);
}
function filledRange(count, prefix, suffix) {
var ret = new Array(count);
for(var i = 0; i < count; ++i) {
ret[i] = prefix + i + suffix;
}
return ret;
}
function safeToString(obj) {
try {
return obj + "";
} catch (e) {
return "[no string representation]";
}
}
function markAsOriginatingFromRejection(e) {
try {
notEnumerableProp(e, "isOperational", true);
}
catch(ignore) {}
}
function originatesFromRejection(e) {
if (e == null) return false;
return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
e["isOperational"] === true);
}
function canAttachTrace(obj) {
return obj instanceof Error && es5.propertyIsWritable(obj, "stack");
}
var ensureErrorObject = (function() {
Iif (!("stack" in new Error())) {
return function(value) {
if (canAttachTrace(value)) return value;
try {throw new Error(safeToString(value));}
catch(err) {return err;}
};
} else {
return function(value) {
if (canAttachTrace(value)) return value;
return new Error(safeToString(value));
};
}
})();
function classString(obj) {
return {}.toString.call(obj);
}
function copyDescriptors(from, to, filter) {
var keys = es5.names(from);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (filter(key)) {
try {
es5.defineProperty(to, key, es5.getDescriptor(from, key));
} catch (ignore) {}
}
}
}
var ret = {
isClass: isClass,
isIdentifier: isIdentifier,
inheritedDataKeys: inheritedDataKeys,
getDataPropertyOrDefault: getDataPropertyOrDefault,
thrower: thrower,
isArray: es5.isArray,
haveGetters: haveGetters,
notEnumerableProp: notEnumerableProp,
isPrimitive: isPrimitive,
isObject: isObject,
canEvaluate: canEvaluate,
errorObj: errorObj,
tryCatch: tryCatch,
inherits: inherits,
withAppended: withAppended,
maybeWrapAsError: maybeWrapAsError,
toFastProperties: toFastProperties,
filledRange: filledRange,
toString: safeToString,
canAttachTrace: canAttachTrace,
ensureErrorObject: ensureErrorObject,
originatesFromRejection: originatesFromRejection,
markAsOriginatingFromRejection: markAsOriginatingFromRejection,
classString: classString,
copyDescriptors: copyDescriptors,
hasDevTools: typeof chrome !== "undefined" && chrome &&
typeof chrome.loadTimes === "function",
isNode: typeof process !== "undefined" &&
classString(process).toLowerCase() === "[object process]"
};
ret.isRecentNode = ret.isNode && (function() {
var version = process.versions.node.split(".").map(Number);
return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
})();
Eif (ret.isNode) ret.toFastProperties(process);
try {throw new Error(); } catch (e) {ret.lastLineError = e;}
module.exports = ret;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| main.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 | 1 | //
// Loader for Strider extension modules.
//
module.exports = require('./lib')
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| basic.js | 40% | (2 / 5) | 0% | (0 / 2) | 0% | (0 / 1) | 40% | (2 / 5) | |
| index.js | 5.26% | (9 / 171) | 2.02% | (2 / 99) | 2.56% | (1 / 39) | 5.92% | (9 / 152) | |
| job.js | 22.22% | (6 / 27) | 0% | (0 / 14) | 0% | (0 / 4) | 23.08% | (6 / 26) | |
| namespace.js | 26.32% | (5 / 19) | 0% | (0 / 6) | 0% | (0 / 3) | 29.41% | (5 / 17) | |
| provider.js | 24.14% | (7 / 29) | 0% | (0 / 12) | 0% | (0 / 5) | 25% | (7 / 28) | |
| runner.js | 33.33% | (4 / 12) | 0% | (0 / 6) | 0% | (0 / 2) | 33.33% | (4 / 12) | |
| utils.js | 11.97% | (14 / 117) | 0% | (0 / 76) | 0% | (0 / 34) | 13.33% | (14 / 105) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 1 1 |
module.exports = {
webapp: webapp
}
function webapp(id, plugin, striderjson, context, done) {
if ('function' !== typeof plugin) {
throw new Error('Invalid basic plugin: ' + id + ' ' + plugin)
}
plugin(context, done)
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 1 1 1 1 1 1 1 1 1 |
var job = require('./job')
, runner = require('./runner')
, provider = require('./provider')
, basic = require('./basic')
, utils = require('./utils')
, fs = require('fs')
, path = require('path')
, async = require('async')
, connect = require('connect')
, _ = require('underscore')
, RESERVED = ['provider', 'config', 'api']
/*
On a grand scale, the things that plugins should be able to do:
// app startup / plugin refresh
- add routes
- attach global listeners
- extend models
*/
module.exports = Loader
function Loader(lesspaths, isNamespaced) {
this.isNamespaced = isNamespaced;
this.types = {
'job': job,
'runner': runner,
'provider': provider,
'basic': basic
};
this.ids = {};
this.lesspaths = lesspaths || [];
this.extensions = {
job: {},
runner: {},
provider: {},
basic: {}
};
}
Loader.prototype = {
// find all extensions in dirs and organize them by type
// load from `strider.json` or package.json's `strider` section
collectExtensions: function (dirs, done) {
var self = this
utils.findExtensions(dirs, function (err, extensions) {
if (err) return done(err)
var type
, fname
for (var i=0; i<extensions.length; i++) {
extensions[i].type = extensions[i].type || 'basic'
extensions[i].id = extensions[i].id.toLowerCase()
if (RESERVED.indexOf(extensions[i].id) !== -1) {
return done(new Error('Extension id "' + extensions[i].id + '" is reserved. Your plugins are misconfigured.'))
}
if (self.ids[extensions[i].id]) {
// XXX should we die hard, ignore, or warn?
return done(new Error('Duplicate extension id! Your plugins are misconfigured.'))
}
self.ids[extensions[i].id] = true
self.extensions[extensions[i].type][extensions[i].id] = extensions[i]
}
done()
})
},
// initialize the "webapp" sections of all plugins.
// initialization depends on the plugin type. See job.js,
// provider.js, runner.js, basic.js
initWebAppExtensions: function (context, done) {
this.initExtensions('webapp', context, done)
},
// initialize the "webapp" sections of all plugins.
// initialization depends on the plugin type. See job.js,
// provider.js, runner.js, basic.js
initWorkerExtensions: function (context, done) {
this.initExtensions('worker', context, done)
},
// initTemplates(done) -> done(err, { "tplname": "template text", ... })
//
// find templates defined by plugins' strider.json config
// looks like:
// "templates": {
// "filetplname": "path/to/tpl.html",
// "inlinetplname": "<div>Hi</div>",
// ...
// }
// Template values that end in ".html" will be read from disk
// relative to the plugin directory
initTemplates: function (done) {
var templates = {}
, self = this
this.allExtensions(function (type, id, plugin, next) {
var tasks = []
if (!plugin.templates) return next()
var task = function (tpl, fname, next) {
fs.readFile(fname, 'utf8', function (err, data) {
if (err) return next(err)
templates[tpl] = data
next()
})
}
for (var tpl in plugin.templates) {
templates[tpl] = plugin.templates[tpl]
if (!/\.html$/.test(plugin.templates[tpl])) continue;
var fname = path.join(plugin.dir, plugin.templates[tpl]);
tasks.push(task.bind(tpl, fname))
}
if (!tasks.length) return next()
async.parallel(tasks, next)
}, function (err) {
done(err, templates)
})
},
// initStaticDirs(app, done) -> done(err)
// go through webapp extensions and if `plugindir/static` is a
// directory, serve up files under the path `/ext/pluginid`
initStaticDirs: function (app, done) {
var self = this
this.allExtensions(function (type, id, plugin, next) {
var staticDir = path.join(plugin.dir, 'static')
fs.stat(staticDir, function (err, stat) {
if (err || !stat || !stat.isDirectory()) return next()
app.use('/ext/' + id, connect.static(staticDir))
next()
})
}, done)
},
// Collect html, css and js from all plugins and compile the css and
// js, writing it to @jspath and @csspath
// done(err)
initConfig: function (jspath, csspath, done) {
if (arguments.length === 1) {
done = jspath
jspath = csspath = null
}
var alljs = []
, allcss = []
, configs = {}
, self = this;
this.allExtensions(function (type, id, plugin, next) {
var namespace = self.isNamespaced ? 'Config.' : '';
if (!plugin.config) {
plugin.config = {}
}
if (!configs[type]) configs[type] = {}
if (plugin.config === true) {
plugin.config = {
savebtn: true
}
}
plugin.config = _.extend({
controller: namespace + type[0].toUpperCase() + type.slice(1) + 'Controller',
/* Defaults. If they don't exist, however, no error is thrown.
template: 'config/config.html',
script: 'config/config.js',
style: 'config/config.less',
*/
icon: plugin.icon,
title: plugin.title || id,
id: id
}, plugin.config)
configs[type][id] = plugin.config
utils.getStatic(id, plugin.dir, plugin.config, self.lesspaths, function (err, html, css, js) {
if (err) return next(err)
plugin.config.html = html || '<h3>This plugin does not require any configuration</h3>'
alljs.push(js)
allcss.push(css)
next()
})
}, function (err) {
if (err) return done(err, configs)
// wrap our javascripts
var jstop = "// GENERATED from the plugins' config files. Do not modify - go to the source.\n" +
"/* jshint undef: false */\n\n"
, jspref = ';(function (angular) {\n'
, jspost = '\n})(angular);\n'
, jstext = jstop + jspref + alljs.join(jspost + jspref) + jspost
, csstext = allcss.join('\n\n')
if (jspath === null && csspath === null) {
return done(null, jstext, csstext, configs)
}
fs.writeFile(jspath, jstext, 'utf8', function (err) {
fs.writeFile(csspath, csstext, 'utf8', function (cerr) {
done(err || cerr, configs)
})
})
})
},
initUserConfig: function (jspath, csspath, done) {
if (arguments.length === 1) {
done = jspath
jspath = csspath = null
}
var alljs = []
, allcss = []
, configs = {}
, types = this.types
, self = this
this.allExtensions(function (type, id, plugin, next) {
var namespace = self.isNamespaced ? 'Account.' : '';
var name = types[type].userConfig || 'user';
var config = name + 'Config';
if (!plugin[config]) return next();
if (!configs[type]) configs[type] = {}
if (plugin[config] === true) {
plugin[config] = {
savebtn: true
}
}
plugin[config] = _.extend({
controller: namespace + type[0].toUpperCase() + type.slice(1) + 'Controller',
/* Defaults. If they don't exist, however, no error is thrown.
template: 'config/user.html',
script: 'config/user.js',
style: 'config/user.less',
*/
inline_icon: plugin.inline_icon,
title: plugin.title,
id: id
}, plugin[config])
configs[type][id] = plugin[config]
utils.getStatic(id, plugin.dir, plugin[config], name, self.lesspaths, function (err, html, css, js) {
if (err) return next(err)
plugin[config].html = html
alljs.push(js)
allcss.push(css)
next()
})
}, function (err) {
if (err) return done(err, configs)
// wrap our javascripts
var jstop = "// GENERATED from the plugins' config files. Do not modify - go to the source.\n" +
"/* jshint undef: false */\n\n"
, jspref = ';(function (angular) {\n'
, jspost = '\n})(angular);\n'
, jstext = jstop + jspref + alljs.join(jspost + jspref) + jspost
, csstext = allcss.join('\n\n')
if (jspath === null && csspath === null) {
return done(null, jstext, csstext, configs)
}
fs.writeFile(jspath, jstext, 'utf8', function (err) {
fs.writeFile(csspath, csstext, 'utf8', function (cerr) {
done(err || cerr, configs)
})
})
})
},
initStatusBlocks: function (jspath, csspath, done) {
if (arguments.length === 1) {
done = jspath
jspath = csspath = null
}
var alljs = []
, allcss = []
, blocks = {}
, types = this.types
, self = this
this.allExtensions(function (type, id, plugin, next) {
var config = plugin['build-status']
if (!config) return next();
if (!blocks[type]) blocks[type] = {}
blocks[type][id] = config
config.id = id
if (type === 'runner') {
config.attrs['ng-show'] = "job.runner.id === '" + id + "'" + (config.attrs['ng-show'] ? ' && ( ' + config.attrs['ng-show'] + ')' : '')
}
config.attrs['ng-show'] = "show" + (config.attrs['ng-show'] ? ' && ' + config.attrs['ng-show'] : '')
utils.getStatic(id, plugin.dir, config, 'build', self.lesspaths, function (err, html, css, js) {
if (err) return next(err)
config.html = html
alljs.push(js)
allcss.push(css)
next()
})
}, function (err) {
if (err) return done(err, blocks)
// wrap our javascripts
var jstop = "// GENERATED from the plugins' config files. Do not modify - go to the source.\n" +
"/* jshint undef: false */\n\n"
, jspref = ';(function (angular) {\n'
, jspost = '\n})(angular);\n'
, jstext = jstop + jspref + alljs.join(jspost + jspref) + jspost
, csstext = allcss.join('\n\n')
if (jspath === null && csspath === null) {
return done(null, jstext, csstext, blocks)
}
fs.writeFile(jspath, jstext, 'utf8', function (err) {
fs.writeFile(csspath, csstext, 'utf8', function (cerr) {
done(err || cerr, blocks)
})
})
})
},
// private
// initExtensions(subtype, context, done) -> done(err, {type: {id: retval, ...}, ...})
// For each plugin
// this.types[type][subtype](id, plugin, context, next(err, retval))
initExtensions: function (subtype, context, done) {
var types = this.types
, loaded = {}
this.allExtensions(function (type, id, plugin, next) {
if (!plugin[subtype]) return next()
if ('string' === typeof plugin[subtype]) {
try {
plugin[subtype] = require(path.resolve(path.join(plugin.dir, plugin[subtype])))
} catch (e) {
console.log('failed to load plugin', id, e.message, e.stack)
e.message += '; error loading plugin ' + id
return next(e)
}
}
if (!loaded[type]) loaded[type] = {}
if (!types[type][subtype]) {
loaded[type][id] = plugin[subtype]
return next()
}
types[type][subtype](id, plugin[subtype], plugin, context, function (err, initialized) {
loaded[type][id] = initialized
next(err)
})
}, function (err) {
done(err, loaded)
})
},
// allExtensions(each, done) -> done(err, [val, ...])
// call `each(type, id, plugin, next(err, val))` for each plugin in parallel
allExtensions: function (each, done) {
var self = this
, tasks = []
Object.keys(this.extensions).forEach(function (type) {
Object.keys(self.extensions[type]).forEach(function (id) {
var plugin = self.extensions[type][id]
tasks.push(function (next) {
each(type, id, plugin, next)
})
})
})
async.parallel(tasks, done)
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 1 1 1 1 1 1 |
var namespace = require('./namespace')
module.exports = {
webapp: webapp
}
function defExtend(dest, src) {
for (var key in src) {
if (!src[key]) continue;
dest[key] = src[key]
}
}
// schema
// {
// routes: function (app, context) {}
// globalRoutes: function (app, context) {}
// listen: function (io, context) {}
// }
function webapp(id, plugin, striderjson, context, done) {
if (plugin.appConfig) {
defExtend(plugin.appConfig, context.config.plugins[id] || {})
}
// setup routes
if (plugin.routes) {
jobRoutes(id, plugin, context)
}
if (plugin.globalRoutes) {
globalRoutes(id, plugin, context)
}
if (plugin.auth) {
plugin.auth(context.passport, context)
}
// listen to global events; most job plugins shouldn't need this
if (plugin.listen) {
plugin.listen(context.emitter, context)
}
done(null, plugin.config)
}
function jobRoutes(id, plugin, context) {
var mid = context.middleware
var app = namespace(context.app, '/:org/:repo/api/' + id, mid.project, mid.projectPlugin.bind(null, id))
app.anon = namespace(context.app, '/:org/:repo/api/' + id, mid.anonProject, mid.projectProvider)
plugin.routes(app, context)
}
function globalRoutes(id, plugin, context) {
var mid = context.middleware
, app = namespace(context.app, '/ext/' + id)
plugin.globalRoutes(app, context)
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 1 1 1 1 |
var methods = require('methods').concat(['del'])
module.exports = namespace
function join(one, two) {
if (one[one.length - 1] === '/') one = one.slice(0, -1)
if (two[0] !== '/') two = '/' + two
return one + two
}
function namespace(app, prefix) {
var middleware = [].slice.call(arguments, 2)
function route(method) {
if (method === 'del') {
method = 'delete';
}
var args = [].slice.call(arguments, 1)
return app[method].apply(app, [join(prefix, args.shift())].concat(middleware.concat(args)))
}
var rep = {}
for (var i=0; i<methods.length; i++) {
rep[methods[i]] = route.bind(null, methods[i])
}
return rep
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 1 1 1 1 1 1 1 |
var namespace = require('./namespace')
module.exports = {
webapp: webapp,
worker: worker,
userConfig: 'account'
}
function worker(id, plugin, striderjson, context, done) {
plugin.hosted = striderjson.hosted
done(null, plugin)
}
function defExtend(dest, src) {
for (var key in src) {
if (!src[key]) continue;
dest[key] = src[key]
}
}
// routes, globalRoutes
function webapp(id, plugin, striderjson, context, done) {
if (plugin.appConfig) {
defExtend(plugin.appConfig, context.config.plugins[id] || {})
}
if (plugin.routes) {
providerRoutes(id, plugin, context)
}
if (plugin.globalRoutes) {
globalRoutes(id, plugin, context)
}
if (plugin.auth) {
plugin.auth(context.passport, context)
}
plugin.hosted = striderjson.hosted
return done(null, plugin)
}
function providerRoutes(id, plugin, context) {
var mid = context.middleware
var app = namespace(context.app, '/:org/:repo/api/' + id, mid.project, mid.projectProvider)
app.anon = namespace(context.app, '/:org/:repo/api/' + id, mid.anonProject, mid.projectProvider)
plugin.routes(app, context)
}
function globalRoutes(id, plugin, context) {
var mid = context.middleware
, app = namespace(context.app, '/ext/' + id)
plugin.globalRoutes(app, context)
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 1 1 1 1 |
var namespace = require('./namespace')
module.exports = {
webapp: webapp
}
function webapp(id, plugin, striderjson, context, done) {
var config = context.config.runners && context.config.runners[id]
, schema = context.models.StriderSchema
/*
if (plugin.appConfig) {
schema.plugin(function (schema, options) {
schema.add(plugin.appConfig, 'runners.' + id)
})
}
*/
// setup routes
if (plugin.globalRoutes) {
globalRoutes(id, plugin, context)
}
// passport authentication
if (plugin.auth) {
plugin.auth(context.passport, context)
}
plugin.create(context.emitter, config, context, done)
}
function globalRoutes(id, plugin, context) {
var app = namespace(context.app, '/ext/' + id)
plugin.globalRoutes(app, context)
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 |
var path = require('path')
, fs = require('fs')
, less = require('less')
, async = require('async')
module.exports = {
checkStatFile: checkStatFile,
checkStrider: checkStrider,
checkPackageJson: checkPackageJson,
checkModule: checkModule,
readDirAbs: readDirAbs,
readDirsParallel: readDirsParallel,
readDirs: readDirs,
checkModules: checkModules,
parseModules: parseModules,
findExtensions: findExtensions,
findAndSortExtensions: findAndSortExtensions,
getStatic: getStatic
}
// config: {template: , style: , script: }
// done(html, css, js)
function getStatic(id, basedir, config, name, lesspaths, done) {
if (arguments.length === 5) {
done = lesspaths
lesspaths = name
name = 'config'
}
async.parallel({
html: function (next) {
var failhard = !!config.template
if (config.template && !config.template.match(/\.html$/)) return next(null, config.template)
fs.readFile(path.join(basedir, config.template || 'config/' + name + '.html'), 'utf8', function (err, data) {
next(failhard && err, data)
})
},
js: function (next) {
var failhard = !!config.script
fs.readFile(path.join(basedir, config.script || 'config/' + name + '.js'), 'utf8', function (err, text) {
if (!failhard && err) return next(null, '')
next(err, text + '\n//# sourceUrl=' + id + '/' + path.join(basedir, config.script || 'config/' + name + '.js') + '\n')
})
},
css: function (next) {
var failhard = !!config.style
fs.readFile(path.join(basedir, config.style || 'config/' + name + '.less'), 'utf8', function (err, text) {
if (!failhard && err) return next(null, '')
var source = '\n/** source: ' + id + '/' + (config.style || 'config/' + name + '.less') + ' **/\n'
if (err) return next(err)
if (config.style && config.style.slice(-5) !== '.less') {
return next(null, text + source)
}
var fpath = path.join(basedir, config.style || name + '.less')
less.render(text, {
paths: [path.dirname(fpath)].concat(lesspaths || []),
filename: fpath
}).then(function(output) {
next(null, output.css + source)
}, function(error) {
next(error)
})
})
}
}, function (err, data) {
return done(err, data.html, data.css, data.js)
})
}
// p is file ? return json content : false
function checkStatFile(p, cb) {
var fullPath = path.resolve(p)
, json
try {
json = require(fullPath)
} catch (e) {
if (e.code == "MODULE_NOT_FOUND") {
return cb(null,false);
} else {
console.error('require("' + fullPath + '") failed', e)
return cb(e)
}
}
return cb(null, json)
}
// checkStrider(pth, cb) -> cb(err, config)
// Check for strider.json. config === false if the file is not present
function checkStrider(pth, cb) {
checkStatFile(path.join(pth, 'strider.json'), function(err, config){
if (err) return cb(err);
if (!config) return cb(null, false);
if (!config.id){
console.error("Error in module: ", pth, " : strider.json must include an 'id'")
return cb(null, false)
}
cb(null, config)
})
}
// checkPackageJson(pth, cb) -> cb(err, config)
// config is the strider section of the package.json or false
// config.id defaults to the name of the package
function checkPackageJson(pth, cb) {
var p = path.join(pth, "package.json")
checkStatFile(p, function(err, pack){
if (err) return cb(err);
if (!pack || !pack.strider) return cb(null, false)
if (!pack.strider.id) {
pack.strider.id = pack.name
}
return cb(null, pack.strider);
})
}
// check for strider extension config in either strider.json or package.json
function checkModule(pth, cb) {
checkStrider(pth, function(err, config){
if (err) return cb(err);
if (config){
config.dir = pth
return cb(null, config)
}
checkPackageJson(pth, function(err, config){
if (err) return cb(err);
if (config){
config.dir = pth
return cb(null, config)
}
return cb(null, false)
})
})
}
// return abs paths
function readDirAbs(dir, cb) {
fs.readdir(dir, function(err, entries) {
if (err || !entries) {
return cb(err, null);
}
var items = [];
entries.forEach(function(entry) {
items.push(path.join(dir, entry))
});
return cb(null, items);
});
}
// fs.readdir list of paths in parallel.
// used when `dir` arg is an array.
function readDirsParallel(dirs, cb) {
var funcs = []
dirs.forEach(function(dir) {
funcs.push(readDirAbs.bind(null, dir));
})
async.parallel(funcs, function(err, results) {
if (err) {
return cb(err, null);
}
// Flatten results and return
var flat = [].concat.apply([], results);
cb(null, flat);
}
);
}
// Get the contents of one or more directories
// if dir is an array, do this in parrallel.
function readDirs(dir, cb) {
// find top-level module dirs
if (Array.isArray(dir)) {
// dir is a list of paths
readDirsParallel(dir, cb)
} else {
// dir is a single path value
readDirAbs(dir, cb)
}
}
function checkModules(modules, cb) {
async.map(modules || [], checkModule, cb);
}
function parseModules(modules, cb) {
var extensions = []
modules.forEach(function(module){
if (module){
module.weight = module.weight || -1
// what does this do?
module.typ = "json"
extensions.push(module)
}
})
return cb(null, extensions)
}
//
// ### Locate Strider Extensions
//
// Under a specified path **dir** look for directories containing file
// 'strider.json'. These are considered Strider modules. **dir** may be either a string or a list of strings.
//
// Or look for package.json with a "strider" key inside. (Preferred way now)
//
// **cb** is a function of signature cb(err, extensions) where extensions is an
// {id : config} mapping on success and err is an error on failure.
//
function findExtensions(dir, cb) {
readDirs(dir, function(err, modules){
checkModules(modules, function(err, extensions){
parseModules(extensions, cb);
})
})
}
function findAndSortExtensions(dir, cb) {
findExtensions(dir, function(err, loaded){
if (err) return cb(err);
// Sort by weight
loaded = loaded.sort(function (a, b) { return a.weight - b.weight; });
cb(null, loaded);
});
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| async.js | 16.51% | (89 / 539) | 4.5% | (10 / 222) | 2.22% | (4 / 180) | 16.64% | (89 / 535) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 1 1 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /*global setImmediate: false, setTimeout: false, console: false */
(function () {
var async = {};
// global on the server, window in the browser
var root, previous_async;
root = this;
Eif (root != null) {
previous_async = root.async;
}
async.noConflict = function () {
root.async = previous_async;
return async;
};
function only_once(fn) {
var called = false;
return function() {
if (called) throw new Error("Callback was already called.");
called = true;
fn.apply(root, arguments);
}
}
//// cross-browser compatiblity functions ////
var _each = function (arr, iterator) {
if (arr.forEach) {
return arr.forEach(iterator);
}
for (var i = 0; i < arr.length; i += 1) {
iterator(arr[i], i, arr);
}
};
var _map = function (arr, iterator) {
if (arr.map) {
return arr.map(iterator);
}
var results = [];
_each(arr, function (x, i, a) {
results.push(iterator(x, i, a));
});
return results;
};
var _reduce = function (arr, iterator, memo) {
if (arr.reduce) {
return arr.reduce(iterator, memo);
}
_each(arr, function (x, i, a) {
memo = iterator(memo, x, i, a);
});
return memo;
};
var _keys = function (obj) {
if (Object.keys) {
return Object.keys(obj);
}
var keys = [];
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
keys.push(k);
}
}
return keys;
};
//// exported async module functions ////
//// nextTick implementation with browser-compatible fallback ////
Iif (typeof process === 'undefined' || !(process.nextTick)) {
if (typeof setImmediate === 'function') {
async.nextTick = function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
async.setImmediate = async.nextTick;
}
else {
async.nextTick = function (fn) {
setTimeout(fn, 0);
};
async.setImmediate = async.nextTick;
}
}
else {
async.nextTick = process.nextTick;
Eif (typeof setImmediate !== 'undefined') {
async.setImmediate = function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
}
else {
async.setImmediate = async.nextTick;
}
}
async.each = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
var completed = 0;
_each(arr, function (x) {
iterator(x, only_once(function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed >= arr.length) {
callback(null);
}
}
}));
});
};
async.forEach = async.each;
async.eachSeries = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
var completed = 0;
var iterate = function () {
iterator(arr[completed], function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed >= arr.length) {
callback(null);
}
else {
iterate();
}
}
});
};
iterate();
};
async.forEachSeries = async.eachSeries;
async.eachLimit = function (arr, limit, iterator, callback) {
var fn = _eachLimit(limit);
fn.apply(null, [arr, iterator, callback]);
};
async.forEachLimit = async.eachLimit;
var _eachLimit = function (limit) {
return function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length || limit <= 0) {
return callback();
}
var completed = 0;
var started = 0;
var running = 0;
(function replenish () {
if (completed >= arr.length) {
return callback();
}
while (running < limit && started < arr.length) {
started += 1;
running += 1;
iterator(arr[started - 1], function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
running -= 1;
if (completed >= arr.length) {
callback();
}
else {
replenish();
}
}
});
}
})();
};
};
var doParallel = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [async.each].concat(args));
};
};
var doParallelLimit = function(limit, fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [_eachLimit(limit)].concat(args));
};
};
var doSeries = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [async.eachSeries].concat(args));
};
};
var _asyncMap = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (err, v) {
results[x.index] = v;
callback(err);
});
}, function (err) {
callback(err, results);
});
};
async.map = doParallel(_asyncMap);
async.mapSeries = doSeries(_asyncMap);
async.mapLimit = function (arr, limit, iterator, callback) {
return _mapLimit(limit)(arr, iterator, callback);
};
var _mapLimit = function(limit) {
return doParallelLimit(limit, _asyncMap);
};
// reduce only has a series version, as doing reduce in parallel won't
// work in many situations.
async.reduce = function (arr, memo, iterator, callback) {
async.eachSeries(arr, function (x, callback) {
iterator(memo, x, function (err, v) {
memo = v;
callback(err);
});
}, function (err) {
callback(err, memo);
});
};
// inject alias
async.inject = async.reduce;
// foldl alias
async.foldl = async.reduce;
async.reduceRight = function (arr, memo, iterator, callback) {
var reversed = _map(arr, function (x) {
return x;
}).reverse();
async.reduce(reversed, memo, iterator, callback);
};
// foldr alias
async.foldr = async.reduceRight;
var _filter = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
async.filter = doParallel(_filter);
async.filterSeries = doSeries(_filter);
// select alias
async.select = async.filter;
async.selectSeries = async.filterSeries;
var _reject = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (!v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
async.reject = doParallel(_reject);
async.rejectSeries = doSeries(_reject);
var _detect = function (eachfn, arr, iterator, main_callback) {
eachfn(arr, function (x, callback) {
iterator(x, function (result) {
if (result) {
main_callback(x);
main_callback = function () {};
}
else {
callback();
}
});
}, function (err) {
main_callback();
});
};
async.detect = doParallel(_detect);
async.detectSeries = doSeries(_detect);
async.some = function (arr, iterator, main_callback) {
async.each(arr, function (x, callback) {
iterator(x, function (v) {
if (v) {
main_callback(true);
main_callback = function () {};
}
callback();
});
}, function (err) {
main_callback(false);
});
};
// any alias
async.any = async.some;
async.every = function (arr, iterator, main_callback) {
async.each(arr, function (x, callback) {
iterator(x, function (v) {
if (!v) {
main_callback(false);
main_callback = function () {};
}
callback();
});
}, function (err) {
main_callback(true);
});
};
// all alias
async.all = async.every;
async.sortBy = function (arr, iterator, callback) {
async.map(arr, function (x, callback) {
iterator(x, function (err, criteria) {
if (err) {
callback(err);
}
else {
callback(null, {value: x, criteria: criteria});
}
});
}, function (err, results) {
if (err) {
return callback(err);
}
else {
var fn = function (left, right) {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
};
callback(null, _map(results.sort(fn), function (x) {
return x.value;
}));
}
});
};
async.auto = function (tasks, callback) {
callback = callback || function () {};
var keys = _keys(tasks);
if (!keys.length) {
return callback(null);
}
var results = {};
var listeners = [];
var addListener = function (fn) {
listeners.unshift(fn);
};
var removeListener = function (fn) {
for (var i = 0; i < listeners.length; i += 1) {
if (listeners[i] === fn) {
listeners.splice(i, 1);
return;
}
}
};
var taskComplete = function () {
_each(listeners.slice(0), function (fn) {
fn();
});
};
addListener(function () {
if (_keys(results).length === keys.length) {
callback(null, results);
callback = function () {};
}
});
_each(keys, function (k) {
var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
var taskCallback = function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
if (err) {
var safeResults = {};
_each(_keys(results), function(rkey) {
safeResults[rkey] = results[rkey];
});
safeResults[k] = args;
callback(err, safeResults);
// stop subsequent errors hitting callback multiple times
callback = function () {};
}
else {
results[k] = args;
async.setImmediate(taskComplete);
}
};
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
var ready = function () {
return _reduce(requires, function (a, x) {
return (a && results.hasOwnProperty(x));
}, true) && !results.hasOwnProperty(k);
};
if (ready()) {
task[task.length - 1](taskCallback, results);
}
else {
var listener = function () {
if (ready()) {
removeListener(listener);
task[task.length - 1](taskCallback, results);
}
};
addListener(listener);
}
});
};
async.waterfall = function (tasks, callback) {
callback = callback || function () {};
if (tasks.constructor !== Array) {
var err = new Error('First argument to waterfall must be an array of functions');
return callback(err);
}
if (!tasks.length) {
return callback();
}
var wrapIterator = function (iterator) {
return function (err) {
if (err) {
callback.apply(null, arguments);
callback = function () {};
}
else {
var args = Array.prototype.slice.call(arguments, 1);
var next = iterator.next();
if (next) {
args.push(wrapIterator(next));
}
else {
args.push(callback);
}
async.setImmediate(function () {
iterator.apply(null, args);
});
}
};
};
wrapIterator(async.iterator(tasks))();
};
var _parallel = function(eachfn, tasks, callback) {
callback = callback || function () {};
if (tasks.constructor === Array) {
eachfn.map(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args);
});
}
}, callback);
}
else {
var results = {};
eachfn.each(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
});
}
};
async.parallel = function (tasks, callback) {
_parallel({ map: async.map, each: async.each }, tasks, callback);
};
async.parallelLimit = function(tasks, limit, callback) {
_parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback);
};
async.series = function (tasks, callback) {
callback = callback || function () {};
if (tasks.constructor === Array) {
async.mapSeries(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args);
});
}
}, callback);
}
else {
var results = {};
async.eachSeries(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
});
}
};
async.iterator = function (tasks) {
var makeCallback = function (index) {
var fn = function () {
if (tasks.length) {
tasks[index].apply(null, arguments);
}
return fn.next();
};
fn.next = function () {
return (index < tasks.length - 1) ? makeCallback(index + 1): null;
};
return fn;
};
return makeCallback(0);
};
async.apply = function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return fn.apply(
null, args.concat(Array.prototype.slice.call(arguments))
);
};
};
var _concat = function (eachfn, arr, fn, callback) {
var r = [];
eachfn(arr, function (x, cb) {
fn(x, function (err, y) {
r = r.concat(y || []);
cb(err);
});
}, function (err) {
callback(err, r);
});
};
async.concat = doParallel(_concat);
async.concatSeries = doSeries(_concat);
async.whilst = function (test, iterator, callback) {
if (test()) {
iterator(function (err) {
if (err) {
return callback(err);
}
async.whilst(test, iterator, callback);
});
}
else {
callback();
}
};
async.doWhilst = function (iterator, test, callback) {
iterator(function (err) {
if (err) {
return callback(err);
}
if (test()) {
async.doWhilst(iterator, test, callback);
}
else {
callback();
}
});
};
async.until = function (test, iterator, callback) {
if (!test()) {
iterator(function (err) {
if (err) {
return callback(err);
}
async.until(test, iterator, callback);
});
}
else {
callback();
}
};
async.doUntil = function (iterator, test, callback) {
iterator(function (err) {
if (err) {
return callback(err);
}
if (!test()) {
async.doUntil(iterator, test, callback);
}
else {
callback();
}
});
};
async.queue = function (worker, concurrency) {
if (concurrency === undefined) {
concurrency = 1;
}
function _insert(q, data, pos, callback) {
if(data.constructor !== Array) {
data = [data];
}
_each(data, function(task) {
var item = {
data: task,
callback: typeof callback === 'function' ? callback : null
};
if (pos) {
q.tasks.unshift(item);
} else {
q.tasks.push(item);
}
if (q.saturated && q.tasks.length === concurrency) {
q.saturated();
}
async.setImmediate(q.process);
});
}
var workers = 0;
var q = {
tasks: [],
concurrency: concurrency,
saturated: null,
empty: null,
drain: null,
push: function (data, callback) {
_insert(q, data, false, callback);
},
unshift: function (data, callback) {
_insert(q, data, true, callback);
},
process: function () {
if (workers < q.concurrency && q.tasks.length) {
var task = q.tasks.shift();
if (q.empty && q.tasks.length === 0) {
q.empty();
}
workers += 1;
var next = function () {
workers -= 1;
if (task.callback) {
task.callback.apply(task, arguments);
}
if (q.drain && q.tasks.length + workers === 0) {
q.drain();
}
q.process();
};
var cb = only_once(next);
worker(task.data, cb);
}
},
length: function () {
return q.tasks.length;
},
running: function () {
return workers;
}
};
return q;
};
async.cargo = function (worker, payload) {
var working = false,
tasks = [];
var cargo = {
tasks: tasks,
payload: payload,
saturated: null,
empty: null,
drain: null,
push: function (data, callback) {
if(data.constructor !== Array) {
data = [data];
}
_each(data, function(task) {
tasks.push({
data: task,
callback: typeof callback === 'function' ? callback : null
});
if (cargo.saturated && tasks.length === payload) {
cargo.saturated();
}
});
async.setImmediate(cargo.process);
},
process: function process() {
if (working) return;
if (tasks.length === 0) {
if(cargo.drain) cargo.drain();
return;
}
var ts = typeof payload === 'number'
? tasks.splice(0, payload)
: tasks.splice(0);
var ds = _map(ts, function (task) {
return task.data;
});
if(cargo.empty) cargo.empty();
working = true;
worker(ds, function () {
working = false;
var args = arguments;
_each(ts, function (data) {
if (data.callback) {
data.callback.apply(null, args);
}
});
process();
});
},
length: function () {
return tasks.length;
},
running: function () {
return working;
}
};
return cargo;
};
var _console_fn = function (name) {
return function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
fn.apply(null, args.concat([function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (typeof console !== 'undefined') {
if (err) {
if (console.error) {
console.error(err);
}
}
else if (console[name]) {
_each(args, function (x) {
console[name](x);
});
}
}
}]));
};
};
async.log = _console_fn('log');
async.dir = _console_fn('dir');
/*async.info = _console_fn('info');
async.warn = _console_fn('warn');
async.error = _console_fn('error');*/
async.memoize = function (fn, hasher) {
var memo = {};
var queues = {};
hasher = hasher || function (x) {
return x;
};
var memoized = function () {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var key = hasher.apply(null, args);
if (key in memo) {
callback.apply(null, memo[key]);
}
else if (key in queues) {
queues[key].push(callback);
}
else {
queues[key] = [callback];
fn.apply(null, args.concat([function () {
memo[key] = arguments;
var q = queues[key];
delete queues[key];
for (var i = 0, l = q.length; i < l; i++) {
q[i].apply(null, arguments);
}
}]));
}
};
memoized.memo = memo;
memoized.unmemoized = fn;
return memoized;
};
async.unmemoize = function (fn) {
return function () {
return (fn.unmemoized || fn).apply(null, arguments);
};
};
async.times = function (count, iterator, callback) {
var counter = [];
for (var i = 0; i < count; i++) {
counter.push(i);
}
return async.map(counter, iterator, callback);
};
async.timesSeries = function (count, iterator, callback) {
var counter = [];
for (var i = 0; i < count; i++) {
counter.push(i);
}
return async.mapSeries(counter, iterator, callback);
};
async.compose = function (/* functions... */) {
var fns = Array.prototype.reverse.call(arguments);
return function () {
var that = this;
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
async.reduce(fns, args, function (newargs, fn, cb) {
fn.apply(that, newargs.concat([function () {
var err = arguments[0];
var nextargs = Array.prototype.slice.call(arguments, 1);
cb(err, nextargs);
}]))
},
function (err, results) {
callback.apply(that, [err].concat(results));
});
};
};
var _applyEach = function (eachfn, fns /*args...*/) {
var go = function () {
var that = this;
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
return eachfn(fns, function (fn, cb) {
fn.apply(that, args.concat([cb]));
},
callback);
};
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 2);
return go.apply(this, args);
}
else {
return go;
}
};
async.applyEach = doParallel(_applyEach);
async.applyEachSeries = doSeries(_applyEach);
async.forever = function (fn, callback) {
function next(err) {
if (err) {
if (callback) {
return callback(err);
}
throw err;
}
fn(next);
}
next();
};
// AMD / RequireJS
Iif (typeof define !== 'undefined' && define.amd) {
define([], function () {
return async;
});
}
// Node.js
else Eif (typeof module !== 'undefined' && module.exports) {
module.exports = async;
}
// included directly via <script> tag
else {
root.async = async;
}
}());
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 1 |
module.exports = [
'get'
, 'post'
, 'put'
, 'head'
, 'delete'
, 'options'
, 'trace'
, 'copy'
, 'lock'
, 'mkcol'
, 'move'
, 'propfind'
, 'proppatch'
, 'unlock'
, 'report'
, 'mkactivity'
, 'checkout'
, 'merge'
, 'm-search'
, 'notify'
, 'subscribe'
, 'unsubscribe'
, 'patch'
];
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| underscore.js | 21.79% | (151 / 693) | 3.42% | (15 / 439) | 9.93% | (15 / 151) | 24.71% | (150 / 607) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6 6 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 5 1 1 1 1 111 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 6 1 1 1 1 111 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 109 109 1 1 1 1 1 1 1 1 1 1 1 7 7 1 3 3 1 | // Underscore.js 1.5.2
// http://underscorejs.org
// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `exports` on the server.
var root = this;
// Save the previous value of the `_` variable.
var previousUnderscore = root._;
// Establish the object that gets returned to break out of a loop iteration.
var breaker = {};
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
// Create quick reference variables for speed access to core prototypes.
var
push = ArrayProto.push,
slice = ArrayProto.slice,
concat = ArrayProto.concat,
toString = ObjProto.toString,
hasOwnProperty = ObjProto.hasOwnProperty;
// All **ECMAScript 5** native function implementations that we hope to use
// are declared here.
var
nativeForEach = ArrayProto.forEach,
nativeMap = ArrayProto.map,
nativeReduce = ArrayProto.reduce,
nativeReduceRight = ArrayProto.reduceRight,
nativeFilter = ArrayProto.filter,
nativeEvery = ArrayProto.every,
nativeSome = ArrayProto.some,
nativeIndexOf = ArrayProto.indexOf,
nativeLastIndexOf = ArrayProto.lastIndexOf,
nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeBind = FuncProto.bind;
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) {
if (obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
};
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `_` as a global object via a string identifier,
// for Closure Compiler "advanced" mode.
Eif (typeof exports !== 'undefined') {
Eif (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
root._ = _;
}
// Current version.
_.VERSION = '1.5.2';
// Collection Functions
// --------------------
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = _.each = _.forEach = function(obj, iterator, context) {
Iif (obj == null) return;
Eif (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
};
// Return the results of applying the iterator to each element.
// Delegates to **ECMAScript 5**'s native `map` if available.
_.map = _.collect = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
each(obj, function(value, index, list) {
results.push(iterator.call(context, value, index, list));
});
return results;
};
var reduceError = 'Reduce of empty array with no initial value';
// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduce && obj.reduce === nativeReduce) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
}
each(obj, function(value, index, list) {
if (!initial) {
memo = value;
initial = true;
} else {
memo = iterator.call(context, memo, value, index, list);
}
});
if (!initial) throw new TypeError(reduceError);
return memo;
};
// The right-associative version of reduce, also known as `foldr`.
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
var initial = arguments.length > 2;
if (obj == null) obj = [];
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
}
var length = obj.length;
if (length !== +length) {
var keys = _.keys(obj);
length = keys.length;
}
each(obj, function(value, index, list) {
index = keys ? keys[--length] : --length;
if (!initial) {
memo = obj[index];
initial = true;
} else {
memo = iterator.call(context, memo, obj[index], index, list);
}
});
if (!initial) throw new TypeError(reduceError);
return memo;
};
// Return the first value which passes a truth test. Aliased as `detect`.
_.find = _.detect = function(obj, iterator, context) {
var result;
any(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) {
result = value;
return true;
}
});
return result;
};
// Return all the elements that pass a truth test.
// Delegates to **ECMAScript 5**'s native `filter` if available.
// Aliased as `select`.
_.filter = _.select = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) results.push(value);
});
return results;
};
// Return all the elements for which a truth test fails.
_.reject = function(obj, iterator, context) {
return _.filter(obj, function(value, index, list) {
return !iterator.call(context, value, index, list);
}, context);
};
// Determine whether all of the elements match a truth test.
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
_.every = _.all = function(obj, iterator, context) {
iterator || (iterator = _.identity);
var result = true;
if (obj == null) return result;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};
// Determine if at least one element in the object matches a truth test.
// Delegates to **ECMAScript 5**'s native `some` if available.
// Aliased as `any`.
var any = _.some = _.any = function(obj, iterator, context) {
iterator || (iterator = _.identity);
var result = false;
if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
each(obj, function(value, index, list) {
if (result || (result = iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};
// Determine if the array or object contains a given value (using `===`).
// Aliased as `include`.
_.contains = _.include = function(obj, target) {
if (obj == null) return false;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
return any(obj, function(value) {
return value === target;
});
};
// Invoke a method (with arguments) on every item in a collection.
_.invoke = function(obj, method) {
var args = slice.call(arguments, 2);
var isFunc = _.isFunction(method);
return _.map(obj, function(value) {
return (isFunc ? method : value[method]).apply(value, args);
});
};
// Convenience version of a common use case of `map`: fetching a property.
_.pluck = function(obj, key) {
return _.map(obj, function(value){ return value[key]; });
};
// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_.where = function(obj, attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return _[first ? 'find' : 'filter'](obj, function(value) {
for (var key in attrs) {
if (attrs[key] !== value[key]) return false;
}
return true;
});
};
// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
_.findWhere = function(obj, attrs) {
return _.where(obj, attrs, true);
};
// Return the maximum element or (element-based computation).
// Can't optimize arrays of integers longer than 65,535 elements.
// See [WebKit Bug 80797](https://bugs.webkit.org/show_bug.cgi?id=80797)
_.max = function(obj, iterator, context) {
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
return Math.max.apply(Math, obj);
}
if (!iterator && _.isEmpty(obj)) return -Infinity;
var result = {computed : -Infinity, value: -Infinity};
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
computed > result.computed && (result = {value : value, computed : computed});
});
return result.value;
};
// Return the minimum element (or element-based computation).
_.min = function(obj, iterator, context) {
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
return Math.min.apply(Math, obj);
}
if (!iterator && _.isEmpty(obj)) return Infinity;
var result = {computed : Infinity, value: Infinity};
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
computed < result.computed && (result = {value : value, computed : computed});
});
return result.value;
};
// Shuffle an array, using the modern version of the
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
_.shuffle = function(obj) {
var rand;
var index = 0;
var shuffled = [];
each(obj, function(value) {
rand = _.random(index++);
shuffled[index - 1] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;
};
// Sample **n** random values from an array.
// If **n** is not specified, returns a single random element from the array.
// The internal `guard` argument allows it to work with `map`.
_.sample = function(obj, n, guard) {
if (arguments.length < 2 || guard) {
return obj[_.random(obj.length - 1)];
}
return _.shuffle(obj).slice(0, Math.max(0, n));
};
// An internal function to generate lookup iterators.
var lookupIterator = function(value) {
return _.isFunction(value) ? value : function(obj){ return obj[value]; };
};
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = function(obj, value, context) {
var iterator = lookupIterator(value);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value: value,
index: index,
criteria: iterator.call(context, value, index, list)
};
}).sort(function(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}), 'value');
};
// An internal function used for aggregate "group by" operations.
var group = function(behavior) {
return function(obj, value, context) {
var result = {};
var iterator = value == null ? _.identity : lookupIterator(value);
each(obj, function(value, index) {
var key = iterator.call(context, value, index, obj);
behavior(result, key, value);
});
return result;
};
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_.groupBy = group(function(result, key, value) {
(_.has(result, key) ? result[key] : (result[key] = [])).push(value);
});
// Indexes the object's values by a criterion, similar to `groupBy`, but for
// when you know that your index values will be unique.
_.indexBy = group(function(result, key, value) {
result[key] = value;
});
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
_.countBy = group(function(result, key) {
_.has(result, key) ? result[key]++ : result[key] = 1;
});
// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator, context) {
iterator = iterator == null ? _.identity : lookupIterator(iterator);
var value = iterator.call(context, obj);
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
iterator.call(context, array[mid]) < value ? low = mid + 1 : high = mid;
}
return low;
};
// Safely create a real, live array from anything iterable.
_.toArray = function(obj) {
if (!obj) return [];
if (_.isArray(obj)) return slice.call(obj);
if (obj.length === +obj.length) return _.map(obj, _.identity);
return _.values(obj);
};
// Return the number of elements in an object.
_.size = function(obj) {
if (obj == null) return 0;
return (obj.length === +obj.length) ? obj.length : _.keys(obj).length;
};
// Array Functions
// ---------------
// Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head` and `take`. The **guard** check
// allows it to work with `_.map`.
_.first = _.head = _.take = function(array, n, guard) {
if (array == null) return void 0;
return (n == null) || guard ? array[0] : slice.call(array, 0, n);
};
// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N. The **guard** check allows it to work with
// `_.map`.
_.initial = function(array, n, guard) {
return slice.call(array, 0, array.length - ((n == null) || guard ? 1 : n));
};
// Get the last element of an array. Passing **n** will return the last N
// values in the array. The **guard** check allows it to work with `_.map`.
_.last = function(array, n, guard) {
if (array == null) return void 0;
if ((n == null) || guard) {
return array[array.length - 1];
} else {
return slice.call(array, Math.max(array.length - n, 0));
}
};
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
// Especially useful on the arguments object. Passing an **n** will return
// the rest N values in the array. The **guard**
// check allows it to work with `_.map`.
_.rest = _.tail = _.drop = function(array, n, guard) {
return slice.call(array, (n == null) || guard ? 1 : n);
};
// Trim out all falsy values from an array.
_.compact = function(array) {
return _.filter(array, _.identity);
};
// Internal implementation of a recursive `flatten` function.
var flatten = function(input, shallow, output) {
if (shallow && _.every(input, _.isArray)) {
return concat.apply(output, input);
}
each(input, function(value) {
if (_.isArray(value) || _.isArguments(value)) {
shallow ? push.apply(output, value) : flatten(value, shallow, output);
} else {
output.push(value);
}
});
return output;
};
// Flatten out an array, either recursively (by default), or just one level.
_.flatten = function(array, shallow) {
return flatten(array, shallow, []);
};
// Return a version of the array that does not contain the specified value(s).
_.without = function(array) {
return _.difference(array, slice.call(arguments, 1));
};
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iterator, context) {
if (_.isFunction(isSorted)) {
context = iterator;
iterator = isSorted;
isSorted = false;
}
var initial = iterator ? _.map(array, iterator, context) : array;
var results = [];
var seen = [];
each(initial, function(value, index) {
if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.contains(seen, value)) {
seen.push(value);
results.push(array[index]);
}
});
return results;
};
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
return _.uniq(_.flatten(arguments, true));
};
// Produce an array that contains every item shared between all the
// passed-in arrays.
_.intersection = function(array) {
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
return _.indexOf(other, item) >= 0;
});
});
};
// Take the difference between one array and a number of other arrays.
// Only the elements present in just the first array will remain.
_.difference = function(array) {
var rest = concat.apply(ArrayProto, slice.call(arguments, 1));
return _.filter(array, function(value){ return !_.contains(rest, value); });
};
// Zip together multiple lists into a single array -- elements that share
// an index go together.
_.zip = function() {
var length = _.max(_.pluck(arguments, "length").concat(0));
var results = new Array(length);
for (var i = 0; i < length; i++) {
results[i] = _.pluck(arguments, '' + i);
}
return results;
};
// Converts lists into objects. Pass either a single array of `[key, value]`
// pairs, or two parallel arrays of the same length -- one of keys, and one of
// the corresponding values.
_.object = function(list, values) {
if (list == null) return {};
var result = {};
for (var i = 0, length = list.length; i < length; i++) {
if (values) {
result[list[i]] = values[i];
} else {
result[list[i][0]] = list[i][1];
}
}
return result;
};
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
// we need this function. Return the position of the first occurrence of an
// item in an array, or -1 if the item is not included in the array.
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
// If the array is large and already in sort order, pass `true`
// for **isSorted** to use binary search.
_.indexOf = function(array, item, isSorted) {
if (array == null) return -1;
var i = 0, length = array.length;
if (isSorted) {
if (typeof isSorted == 'number') {
i = (isSorted < 0 ? Math.max(0, length + isSorted) : isSorted);
} else {
i = _.sortedIndex(array, item);
return array[i] === item ? i : -1;
}
}
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted);
for (; i < length; i++) if (array[i] === item) return i;
return -1;
};
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
_.lastIndexOf = function(array, item, from) {
if (array == null) return -1;
var hasIndex = from != null;
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) {
return hasIndex ? array.lastIndexOf(item, from) : array.lastIndexOf(item);
}
var i = (hasIndex ? from : array.length);
while (i--) if (array[i] === item) return i;
return -1;
};
// Generate an integer Array containing an arithmetic progression. A port of
// the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range).
_.range = function(start, stop, step) {
if (arguments.length <= 1) {
stop = start || 0;
start = 0;
}
step = arguments[2] || 1;
var length = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0;
var range = new Array(length);
while(idx < length) {
range[idx++] = start;
start += step;
}
return range;
};
// Function (ahem) Functions
// ------------------
// Reusable constructor function for prototype setting.
var ctor = function(){};
// Create a function bound to a given object (assigning `this`, and arguments,
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
// available.
_.bind = function(func, context) {
var args, bound;
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError;
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
ctor.prototype = null;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) return result;
return self;
};
};
// Partially apply a function by creating a version that has had some of its
// arguments pre-filled, without changing its dynamic `this` context.
_.partial = function(func) {
var args = slice.call(arguments, 1);
return function() {
return func.apply(this, args.concat(slice.call(arguments)));
};
};
// Bind all of an object's methods to that object. Useful for ensuring that
// all callbacks defined on an object belong to it.
_.bindAll = function(obj) {
var funcs = slice.call(arguments, 1);
if (funcs.length === 0) throw new Error("bindAll must be passed function names");
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
return obj;
};
// Memoize an expensive function by storing its results.
_.memoize = function(func, hasher) {
var memo = {};
hasher || (hasher = _.identity);
return function() {
var key = hasher.apply(this, arguments);
return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};
// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
var args = slice.call(arguments, 2);
return setTimeout(function(){ return func.apply(null, args); }, wait);
};
// Defers a function, scheduling it to run after the current call stack has
// cleared.
_.defer = function(func) {
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
};
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
options || (options = {});
var later = function() {
previous = options.leading === false ? 0 : new Date;
timeout = null;
result = func.apply(context, args);
};
return function() {
var now = new Date;
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
return function() {
context = this;
args = arguments;
timestamp = new Date();
var later = function() {
var last = (new Date()) - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) result = func.apply(context, args);
}
};
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) result = func.apply(context, args);
return result;
};
};
// Returns a function that will be executed at most one time, no matter how
// often you call it. Useful for lazy initialization.
_.once = function(func) {
var ran = false, memo;
return function() {
if (ran) return memo;
ran = true;
memo = func.apply(this, arguments);
func = null;
return memo;
};
};
// Returns the first function passed as an argument to the second,
// allowing you to adjust arguments, run code before and after, and
// conditionally execute the original function.
_.wrap = function(func, wrapper) {
return function() {
var args = [func];
push.apply(args, arguments);
return wrapper.apply(this, args);
};
};
// Returns a function that is the composition of a list of functions, each
// consuming the return value of the function that follows.
_.compose = function() {
var funcs = arguments;
return function() {
var args = arguments;
for (var i = funcs.length - 1; i >= 0; i--) {
args = [funcs[i].apply(this, args)];
}
return args[0];
};
};
// Returns a function that will only be executed after being called N times.
_.after = function(times, func) {
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};
// Object Functions
// ----------------
// Retrieve the names of an object's properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
};
// Retrieve the values of an object's properties.
_.values = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var values = new Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
};
// Convert an object into a list of `[key, value]` pairs.
_.pairs = function(obj) {
var keys = _.keys(obj);
var length = keys.length;
var pairs = new Array(length);
for (var i = 0; i < length; i++) {
pairs[i] = [keys[i], obj[keys[i]]];
}
return pairs;
};
// Invert the keys and values of an object. The values must be serializable.
_.invert = function(obj) {
var result = {};
var keys = _.keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
};
// Return a sorted list of the function names available on the object.
// Aliased as `methods`
_.functions = _.methods = function(obj) {
var names = [];
for (var key in obj) {
if (_.isFunction(obj[key])) names.push(key);
}
return names.sort();
};
// Extend a given object with all the properties in passed-in object(s).
_.extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
Eif (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
// Return a copy of the object only containing the whitelisted properties.
_.pick = function(obj) {
var copy = {};
var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
each(keys, function(key) {
if (key in obj) copy[key] = obj[key];
});
return copy;
};
// Return a copy of the object without the blacklisted properties.
_.omit = function(obj) {
var copy = {};
var keys = concat.apply(ArrayProto, slice.call(arguments, 1));
for (var key in obj) {
if (!_.contains(keys, key)) copy[key] = obj[key];
}
return copy;
};
// Fill in a given object with default properties.
_.defaults = function(obj) {
each(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
if (obj[prop] === void 0) obj[prop] = source[prop];
}
}
});
return obj;
};
// Create a (shallow-cloned) duplicate of an object.
_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};
// Invokes interceptor with the obj, and then returns obj.
// The primary purpose of this method is to "tap into" a method chain, in
// order to perform operations on intermediate results within the chain.
_.tap = function(obj, interceptor) {
interceptor(obj);
return obj;
};
// Internal recursive comparison function for `isEqual`.
var eq = function(a, b, aStack, bStack) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
if (a === b) return a !== 0 || 1 / a == 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a == null || b == null) return a === b;
// Unwrap any wrapped objects.
if (a instanceof _) a = a._wrapped;
if (b instanceof _) b = b._wrapped;
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className != toString.call(b)) return false;
switch (className) {
// Strings, numbers, dates, and booleans are compared by value.
case '[object String]':
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
// equivalent to `new String("5")`.
return a == String(b);
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
// other numeric values.
return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a == +b;
// RegExps are compared by their source patterns and flags.
case '[object RegExp]':
return a.source == b.source &&
a.global == b.global &&
a.multiline == b.multiline &&
a.ignoreCase == b.ignoreCase;
}
if (typeof a != 'object' || typeof b != 'object') return false;
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] == a) return bStack[length] == b;
}
// Objects with different constructors are not equivalent, but `Object`s
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
_.isFunction(bCtor) && (bCtor instanceof bCtor))) {
return false;
}
// Add the first object to the stack of traversed objects.
aStack.push(a);
bStack.push(b);
var size = 0, result = true;
// Recursively compare objects and arrays.
if (className == '[object Array]') {
// Compare array lengths to determine if a deep comparison is necessary.
size = a.length;
result = size == b.length;
if (result) {
// Deep compare the contents, ignoring non-numeric properties.
while (size--) {
if (!(result = eq(a[size], b[size], aStack, bStack))) break;
}
}
} else {
// Deep compare objects.
for (var key in a) {
if (_.has(a, key)) {
// Count the expected number of properties.
size++;
// Deep compare each member.
if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
}
}
// Ensure that both objects contain the same number of properties.
if (result) {
for (key in b) {
if (_.has(b, key) && !(size--)) break;
}
result = !size;
}
}
// Remove the first object from the stack of traversed objects.
aStack.pop();
bStack.pop();
return result;
};
// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
return eq(a, b, [], []);
};
// Is a given array, string, or object empty?
// An "empty" object has no enumerable own-properties.
_.isEmpty = function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (_.has(obj, key)) return false;
return true;
};
// Is a given value a DOM element?
_.isElement = function(obj) {
return !!(obj && obj.nodeType === 1);
};
// Is a given value an array?
// Delegates to ECMA5's native Array.isArray
_.isArray = nativeIsArray || function(obj) {
return toString.call(obj) == '[object Array]';
};
// Is a given variable an object?
_.isObject = function(obj) {
return obj === Object(obj);
};
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
_['is' + name] = function(obj) {
return toString.call(obj) == '[object ' + name + ']';
};
});
// Define a fallback version of the method in browsers (ahem, IE), where
// there isn't any inspectable "Arguments" type.
Iif (!_.isArguments(arguments)) {
_.isArguments = function(obj) {
return !!(obj && _.has(obj, 'callee'));
};
}
// Optimize `isFunction` if appropriate.
Eif (typeof (/./) !== 'function') {
_.isFunction = function(obj) {
return typeof obj === 'function';
};
}
// Is a given object a finite number?
_.isFinite = function(obj) {
return isFinite(obj) && !isNaN(parseFloat(obj));
};
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
_.isNaN = function(obj) {
return _.isNumber(obj) && obj != +obj;
};
// Is a given value a boolean?
_.isBoolean = function(obj) {
return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
};
// Is a given value equal to null?
_.isNull = function(obj) {
return obj === null;
};
// Is a given variable undefined?
_.isUndefined = function(obj) {
return obj === void 0;
};
// Shortcut function for checking if an object has a given property directly
// on itself (in other words, not on a prototype).
_.has = function(obj, key) {
return hasOwnProperty.call(obj, key);
};
// Utility Functions
// -----------------
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
// previous owner. Returns a reference to the Underscore object.
_.noConflict = function() {
root._ = previousUnderscore;
return this;
};
// Keep the identity function around for default iterators.
_.identity = function(value) {
return value;
};
// Run a function **n** times.
_.times = function(n, iterator, context) {
var accum = Array(Math.max(0, n));
for (var i = 0; i < n; i++) accum[i] = iterator.call(context, i);
return accum;
};
// Return a random integer between min and max (inclusive).
_.random = function(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
};
// List of HTML entities for escaping.
var entityMap = {
escape: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}
};
entityMap.unescape = _.invert(entityMap.escape);
// Regexes containing the keys and values listed immediately above.
var entityRegexes = {
escape: new RegExp('[' + _.keys(entityMap.escape).join('') + ']', 'g'),
unescape: new RegExp('(' + _.keys(entityMap.unescape).join('|') + ')', 'g')
};
// Functions for escaping and unescaping strings to/from HTML interpolation.
_.each(['escape', 'unescape'], function(method) {
_[method] = function(string) {
if (string == null) return '';
return ('' + string).replace(entityRegexes[method], function(match) {
return entityMap[method][match];
});
};
});
// If the value of the named `property` is a function then invoke it with the
// `object` as context; otherwise, return it.
_.result = function(object, property) {
if (object == null) return void 0;
var value = object[property];
return _.isFunction(value) ? value.call(object) : value;
};
// Add your own custom functions to the Underscore object.
_.mixin = function(obj) {
each(_.functions(obj), function(name) {
var func = _[name] = obj[name];
_.prototype[name] = function() {
var args = [this._wrapped];
push.apply(args, arguments);
return result.call(this, func.apply(_, args));
};
});
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
var idCounter = 0;
_.uniqueId = function(prefix) {
var id = ++idCounter + '';
return prefix ? prefix + id : id;
};
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(text, data, settings) {
var render;
settings = _.defaults({}, settings, _.templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = new RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
}
if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
}
if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
index = offset + match.length;
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + "return __p;\n";
try {
render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
}
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
};
// Provide the compiled function source as a convenience for precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
// Add a "chain" function, which will delegate to the wrapper.
_.chain = function(obj) {
return _(obj).chain();
};
// OOP
// ---------------
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
// Helper function to continue chaining intermediate results.
var result = function(obj) {
return this._chain ? _(obj).chain() : obj;
};
// Add all of the Underscore functions to the wrapper object.
_.mixin(_);
// Add all mutator Array functions to the wrapper.
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
var method = ArrayProto[name];
_.prototype[name] = function() {
var obj = this._wrapped;
method.apply(obj, arguments);
if ((name == 'shift' || name == 'splice') && obj.length === 0) delete obj[0];
return result.call(this, obj);
};
});
// Add all accessor Array functions to the wrapper.
each(['concat', 'join', 'slice'], function(name) {
var method = ArrayProto[name];
_.prototype[name] = function() {
return result.call(this, method.apply(this._wrapped, arguments));
};
});
_.extend(_.prototype, {
// Start chaining a wrapped Underscore object.
chain: function() {
this._chain = true;
return this;
},
// Extracts the result from a wrapped and chained object.
value: function() {
return this._wrapped;
}
});
}).call(this);
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| mailer.js | 27.59% | (16 / 58) | 12.9% | (4 / 31) | 14.29% | (1 / 7) | 27.59% | (16 / 58) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | var debug = require('debug')('strider-mailer');
var each = require('lodash.foreach');
var everypaas = require('everypaas');
var nodemailer = require('nodemailer');
module.exports = function (config) {
/*
* opening smtp connection
*/
// Default to printing a warning
var smtpTransport = {
sendMail: function (opts, cb) {
debug('WARNING: no SMTP transport detected nor configured. Cannot send email.');
cb(null, {message: null});
}
};
// Try using SendGrid / Mailgun
Iif (everypaas.getSMTP() !== null) {
debug('Using SMTP transport: %j', everypaas.getSMTP());
smtpTransport = nodemailer.createTransport.apply(null, everypaas.getSMTP());
} else {
Iif (config.sendgrid) {
debug('Using Sendgrid transport from config');
smtpTransport = nodemailer.createTransport('SMTP', {
service: 'SendGrid',
auth: {
user: config.sendgrid.username,
pass: config.sendgrid.password
}
});
} else Iif (config.smtp) {
debug('Using SMTP transport from config');
var smtp = config.smtp;
var smtpConfig = {
host: smtp.host,
port: parseInt(smtp.port, 10)
};
// enable secureConnection is port is 465 to use encrypted handshake
if (smtpConfig.port == 465) {
smtpConfig.secureConnection = true;
}
// allow anonymous SMTP login if user and pass are not defined
if (smtp.auth && smtp.auth.user && smtp.auth.pass) {
smtpConfig.auth = {
user: smtp.auth.user,
pass: smtp.auth.pass
};
}
smtpTransport = nodemailer.createTransport('SMTP', smtpConfig);
} else Eif (config.stubSmtp) {
debug('stubbing smtp..');
smtpTransport = nodemailer.createTransport('Stub');
}
}
function send(to, subject, textBody, htmlBody, from, callback) {
from = from || (config.smtp ? config.smtp.from : null);
var mailOptions = {
from: from, // sender address
to: to, // list of receivers
subject: subject, // Subject line
text: textBody, // plaintext body_template
html: htmlBody // html body
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function (error, response) {
if (error) {
debug('Error sending email: ', error);
}
if (config.stubSmtp) {
debug(response.message);
}
if (callback) {
callback(error, response);
}
});
}
/*
* format_stdmerged()
*
* Format the stdmerged property (test std stream output) for sendgrid consumption.
* <stdmerged> - Job's stdmerged property.
*/
function format_stdmerged(stdmerged, emailFormat) {
// 4k
var start = stdmerged.length - 1 - 4096;
if (start < 0) {
start = 0;
}
var tlog = stdmerged.slice(start, stdmerged.length - 1).replace(/^\s+|\s+$/g, '');
// Start each line with a space
var tlines = tlog.split('\n');
var b = new Buffer(8192);
var offset = 0;
each(tlines, function (l) {
var towrite;
if (emailFormat === 'plaintext') {
towrite = ' ' + l.replace(/\[(\d)?\d*m/gi, '') + '\n';
} else {
towrite = ' ' + l.replace(/\[(\d)?\d*m/gi, '') + '<br>\n';
}
b.write(towrite, offset, towrite.length);
offset += towrite.length;
});
return b.toString('utf8', 0, offset);
}
function elapsed_time(start, finish) {
var inSeconds = (finish - start) / 1000;
if (inSeconds > 60) {
return (Math.floor(inSeconds / 60) + 'm ' + Math.round(inSeconds % 60) + 's');
} else {
return (Math.round(inSeconds) + 's');
}
}
return {
send: send,
format_stdmerged: format_stdmerged,
elapsed_time: elapsed_time
};
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 45.05% | (41 / 91) | 27.59% | (16 / 58) | 63.64% | (7 / 11) | 45.56% | (41 / 90) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | var fs = require('fs')
function EveryPaaS() {
this.DOTCLOUD = "dotcloud"
this.HEROKU = "heroku"
this.NODEJITSU = "nodejitsu"
this.NONE = "none"
this.STRIDER = "strider"
this.detect()
}
EveryPaaS.prototype.detect = function(env, dcf) {
var env = env || process.env
var dotCloudFilename = dcf || "/home/dotcloud/environment.json"
this.paas = this.NONE
this.herokuEnvironment = undefined
this.striderEnvironment = undefined
try {
this.getDotCloud(dotCloudFilename)
this.paas = this.DOTCLOUD
return this.paas
} catch(e) {
Iif (this.isHeroku(env)) {
this.herokuEnvironment = env
this.paas = this.HEROKU
return this.paas
}
Iif (this.isStrider(env)) {
this.striderEnvironment = env
this.paas = this.STRIDER
return this.paas
}
Iif (this.isNodejitsu()) {
this.paas = this.NODEJITSU
return this.paas
}
return this.paas
}
}
EveryPaaS.prototype.isHeroku = function (env) {
var env = env || this.herokuEnvironment
return (env && (env.PORT !== undefined && env.PAAS_NAME !== "strider"))
}
EveryPaaS.prototype.getDotCloud = function(filename) {
Iif (this.dotCloudEnvironment !== undefined) return this.dotCloudEnvironment
try {
var data = fs.readFileSync(filename)
this.dotCloudEnvironment = JSON.parse(data)
return this.dotCloudEnvironment
} catch(e) {
throw new Error("could not get dotcloud environment")
}
}
EveryPaaS.prototype.isDotCloud = function() {
return (this.dotCloudEnvironment !== undefined)
}
EveryPaaS.prototype.isNodejitsu = function() {
}
EveryPaaS.prototype.isStrider = function(env) {
var env = env || this.striderEnvironment
return (env && env.PAAS_NAME !== undefined
&& env.PAAS_NAME.toLowerCase() === "strider")
}
EveryPaaS.prototype.getMongodbUrl = function() {
if (this.isDotCloud()) {
return getDotCloudVar(this.dotCloudEnvironment, "MONGODB_URL")
}
if (this.isHeroku()) {
if (this.herokuEnvironment.MONGOLAB_URI)
return this.herokuEnvironment.MONGOLAB_URI
if (this.herokuEnvironment.MONGOHQ_URL)
return this.herokuEnvironment.MONGOHQ_URL
return null
}
if (this.isStrider()) {
if (this.striderEnvironment.MONGODB_URL)
return this.striderEnvironment.MONGODB_URL
return null
}
return null
}
//
// ## Return an argument list which can be applied to nodemailer's createTransport function
//
EveryPaaS.prototype.getSMTP = function() {
Iif (this.isHeroku()) {
var res = {}
if (this.herokuEnvironment.SENDGRID_USERNAME && this.herokuEnvironment.SENDGRID_PASSWORD) {
return ["SMTP",{
service: "SendGrid",
auth: {
user: this.herokuEnvironment.SENDGRID_USERNAME,
pass: this.herokuEnvironment.SENDGRID_PASSWORD
}
}]
}
if (this.herokuEnvironment.MAILGUN_SMTP_SERVER) {
return ["SMTP",{
host:this.herokuEnvironment.MAILGUN_SMTP_SERVER,
port:parseInt(this.herokuEnvironment.MAILGUN_SMTP_PORT),
auth: {
user: this.herokuEnvironment.MAILGUN_SMTP_LOGIN,
pass: this.herokuEnvironment.MAILGUN_SMTP_PASSWORD
}
}]
}
return null
}
return null
}
EveryPaaS.prototype.getMysqlUrl = function() {
if (this.isDotCloud()) {
return getDotCloudVar(this.dotCloudEnvironment, "MYSQL_URL")
}
if (this.isHeroku()) {
if (this.herokuEnvironment.CLEARDB_DATABASE_URL)
return this.herokuEnvironment.CLEARDB_DATABASE_URL
if (this.herokuEnvironment.XEROUND_DATABASE_INTERNAL_URL)
return this.herokuEnvironment.XEROUND_DATABASE_INTERNAL_URL
return null
}
if (this.isStrider()) {
if (this.striderEnvironment.MYSQL_URL)
return this.striderEnvironment.MYSQL_URL
return null
}
return null
}
function getDotCloudVar(dotCloudEnvironment, key) {
for (var k in dotCloudEnvironment) {
if (k.indexOf("DOTCLOUD_") === 0
&& k.lastIndexOf(key) !== -1) {
return dotCloudEnvironment[k]
}
}
return null
}
// Run detection when module loaded.
var ep = new EveryPaaS()
module.exports = ep
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var path = require('path');
var passport = require('passport');
var async = require('async');
var Loader = require('strider-extension-loader');
var globalTunnel = require('global-tunnel');
var app = require('./lib/app');
var common = require('./lib/common');
var config = require('./lib/config');
var middleware = require('./lib/middleware');
var auth = require('./lib/auth');
var models = require('./lib/models');
var pluginTemplates = require('./lib/plugin-templates');
var upgrade = require('./lib/models/upgrade').ensure;
var loadExtensions = require('./lib/utils/load-extensions');
var killZombies = require('./lib/utils/kill-zombies');
var registerPanel = require('./lib/utils/register-panel');
var Job = models.Job;
var Config = models.Config;
common.extensions = {};
//
// Use globa-tunnel to provide proxy support.
// The http_proxy environment variable will be used if the first parameter to globalTunnel.initialize is null.
//
globalTunnel.initialize();
module.exports = function (extdir, c, callback) {
var appConfig = config;
var k;
// override with c
for (k in c) {
appConfig[k] = c[k];
}
// Initialize the (web) app
var appInstance = app.init(appConfig);
var cb = callback || defaultCallback;
function defaultCallback(err) {
if (err) {
throw err;
}
}
if (typeof Loader !== 'function') {
throw new Error('Your version of strider-extension-loader is out of date');
}
var loader = new Loader([path.join(__dirname, 'client/styles')], true);
appInstance.loader = loader;
common.loader = loader;
//
// ### Strider Context Object
//
// Context object is passed to each extension. It carries various config
// settings, as well as handles to enable functions to register things.
// Context can also be accessed as a singleton within Strider as
// common.context.
var context = {
serverName: appConfig.server_name,
config: appConfig,
enablePty: config.enablePty,
emitter: common.emitter,
extensionRoutes: [],
extensionPaths: extdir,
extdir: extdir,
loader: loader,
models: models,
logger: console,
middleware: middleware,
auth: auth, //TODO - may want to make this a subset of the auth module
passport: passport,
registerPanel: registerPanel(common),
registerBlock: pluginTemplates.registerBlock,
app: appInstance
};
// Make extension context available throughout application.
common.context = context;
var SCHEMA_VERSION = Config.SCHEMA_VERSION;
upgrade(SCHEMA_VERSION, function (err) {
if (err) {
return cb(err);
}
loadExtensions(loader, extdir, context, appInstance, function () {
// kill zombie jobs
killZombies(function () {
var tasks = [];
if (!common.extensions.runner || typeof common.extensions.runner !== 'object') {
console.error('Strider seems to have been misconfigured - there are no available runner plugins. ' +
'Please make sure all dependencies are up to date.');
process.exit(1);
}
Object.keys(common.extensions.runner).forEach(function (name) {
var runner = common.extensions.runner[name];
if (!runner) {
console.log('no runner', name);
return;
}
tasks.push(function (next) {
Job.find({
'runner.id': name,
finished: null
}, function (error, jobs) {
if (error) {
return next(error);
}
runner.findZombies(jobs, next);
});
});
});
async.parallel(tasks, function (err, zombies) {
if (err) return cb(err);
var ids = [].concat.apply([], zombies).map(function (job) {
return job._id;
});
var now = new Date();
Job.update({_id: {$in: ids}}, {
$set: {
finished: now,
errored: true,
error: {message: 'Job timeout', stack: ''}
}
}, function () {
Job.update({_id: {$in: ids}, started: null}, {$set: {started: now}}, function (err) {
cb(err, appInstance);
});
});
});
});
});
});
return appInstance;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 1 | 'use strict';
module.exports = {
jquery: {
exports: 'jQuery'
},
bootstrap: {
depends: {
jquery: 'jQuery'
}
},
sortable: {
exports: 'Sortable'
},
ngSortableDirective: {
exports: 'ngSortableDirective',
depends: {
angular: 'angular',
sortable: 'Sortable'
}
},
'ui-bootstrap': {
depends: {
angular: 'angular'
}
},
md5: {
exports: 'md5'
},
bootbox: {
exports: 'bootbox'
},
codemirror: {
exports: 'CodeMirror'
},
'codemirror-shell': {
depends: {
codemirror: 'CodeMirror'
}
},
'ui-codemirror': {
depends: {
angular: 'angular',
codemirror: 'CodeMirror',
'codemirror-shell': null
}
},
timeago: {
depends: {
jquery: 'jQuery'
}
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| api_data.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| api_project.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 | 2 | define({ "api": [
{
"type": "post",
"url": "/account/email",
"title": "Change Email",
"description": "<p>Changes the email address for the <em>active</em> user (the API user).</p>",
"name": "ChangeEmail",
"group": "Account",
"version": "1.0.0",
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "email",
"description": "<p>The new email address. This must be a VALID email address.</p>"
}
]
}
},
"filename": "lib/routes/api/account.js",
"groupTitle": "Account"
},
{
"type": "post",
"url": "/account/password",
"title": "Change Password",
"description": "<p>Changes the password for the <em>active</em> user (the API user).</p>",
"name": "ChangePassword",
"group": "Account",
"version": "1.0.0",
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"size": "6..",
"optional": false,
"field": "password",
"description": "<p>The new password, which must be at least 6 characters long.</p>"
}
]
}
},
"filename": "lib/routes/api/account.js",
"groupTitle": "Account"
},
{
"type": "delete",
"url": "/account/:provider/:id",
"title": "Delete Provider Account",
"description": "<p>Deletes a provider account for the <em>active</em> user (the API user).</p>",
"name": "DeleteAccount",
"group": "Account",
"version": "1.0.0",
"parameter": {
"fields": {
"Parameter": [
{
"group": "Parameter",
"type": "String",
"optional": false,
"field": "provider",
"description": "<p>Type of provider, e.g. github</p>"
},
{
"group": "Parameter",
"type": "Number",
"optional": false,
"field": "id",
"description": "<p>Unique provider identification</p>"
}
]
}
},
"filename": "lib/routes/api/account.js",
"groupTitle": "Account"
},
{
"type": "put",
"url": "/account/:provider/:id",
"title": "Update Provider Account",
"description": "<p>Updates a provider account for the <em>active</em> user (the API user).</p>",
"name": "UpdateAccount",
"group": "Account",
"version": "1.0.0",
"parameter": {
"fields": {
"Parameter": [
{
"group": "Parameter",
"type": "String",
"optional": false,
"field": "provider",
"description": "<p>Type of provider, e.g. github</p>"
},
{
"group": "Parameter",
"type": "Number",
"optional": false,
"field": "id",
"description": "<p>Unique provider identification</p>"
}
]
}
},
"filename": "lib/routes/api/account.js",
"groupTitle": "Account"
},
{
"type": "get",
"url": "/admin/users",
"title": "Get All Users",
"permission": [
{
"name": "GlobalAdmin",
"title": "You must have admin privileges in Strider (globally) in order to use this endpoint.",
"description": ""
}
],
"description": "<p>Retrieves a list of all Strider users.</p>",
"name": "GetUsers",
"group": "Admin",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X GET http://localhost/admin/users",
"type": "curl"
}
],
"filename": "lib/routes/api/admin/index.js",
"groupTitle": "Admin"
},
{
"type": "post",
"url": "/admin/invite/revoke",
"title": "Revoke Invite",
"permission": [
{
"name": "GlobalAdmin",
"title": "You must have admin privileges in Strider (globally) in order to use this endpoint.",
"description": ""
}
],
"description": "<p>Revokes a previously sent Strider invitation.</p>",
"name": "RevokeInvite",
"group": "Admin",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X POST -d invite_code=xoxox http://localhost/invite/revoke",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "invite_code",
"description": "<p>The invite code/token of the invite being revoked.</p>"
}
]
}
},
"filename": "lib/routes/api/admin/index.js",
"groupTitle": "Admin"
},
{
"type": "post",
"url": "/admin/invite/new",
"title": "Send Invite",
"permission": [
{
"name": "GlobalAdmin",
"title": "You must have admin privileges in Strider (globally) in order to use this endpoint.",
"description": ""
}
],
"description": "<p>Create & email a new Strider invite.</p>",
"name": "SendInvite",
"group": "Admin",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X POST -d invite_code=xoxox -d email=new_guy@strider-cd.com http://localhost/invite/new",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "invite_code",
"description": "<p>The invite code/token to use in the invitation</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "email",
"description": "<p>The email address of the new user being invited</p>"
}
]
}
},
"filename": "lib/routes/api/admin/index.js",
"groupTitle": "Admin"
},
{
"type": "post",
"url": "/:org/:repo/branches",
"title": "Add Branch",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Add a new branch for a project.</p>",
"name": "AddBranch",
"group": "Branch",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X POST -d name=newbranch http://localhost/api/strider-cd/strider/branches",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "name",
"description": "<p>The name of the new branch</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "cloneName",
"description": "<p>The name of the cloned branch</p>"
}
],
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
},
"filename": "lib/routes/api/branches.js",
"groupTitle": "Branch"
},
{
"type": "delete",
"url": "/:org/:repo/branches",
"title": "Delete Branch",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Deletes a branch from a project</p>",
"name": "DeleteBranch",
"group": "Branch",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X DELETE -d name=mybranch http://localhost/api/strider-cd/strider/branches",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "name",
"description": "<p>The name of the branch to delete</p>"
}
],
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
},
"filename": "lib/routes/api/branches.js",
"groupTitle": "Branch"
},
{
"type": "put",
"url": "/:org/:repo/branches",
"title": "Reorder Branches",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Updates the branch order for a project.</p>",
"name": "ReorderBranches",
"group": "Branch",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X PUT -d branches=master,testing http://localhost/api/strider-cd/strider/branches",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "branches",
"description": "<p>The new branch order, comma delimited</p>"
}
],
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
},
"filename": "lib/routes/api/branches.js",
"groupTitle": "Branch"
},
{
"type": "post",
"url": "/:org/:repo/collaborators",
"title": "Add Collaborator",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Add a new collaborator to a repository/project.</p>",
"name": "AddCollaborator",
"group": "Collaborators",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X GET -d '{\"email\":\"new_guy@strider-cd.com\", \"access\":2}' http://localhost/api/strider-cd/strider/collaborators",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "email",
"description": "<p>Email address to add. If the user is not registered with Strider, we will send them an invite. If they are already registered, they will receive a notification of access.</p>"
},
{
"group": "RequestBody",
"type": "Number",
"optional": false,
"field": "access",
"defaultValue": "0",
"description": "<p>Access level to grant to the new collaborator. This can be <code>0</code>, for read only access, or <code>2</code> for admin access.</p>"
}
],
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
},
"filename": "lib/routes/collaborators/index.js",
"groupTitle": "Collaborators"
},
{
"type": "delete",
"url": "/:org/:repo/collaborators",
"title": "Delete Collaborator",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Remove a collaborator from a repository/project.</p>",
"name": "DeleteCollaborator",
"group": "Collaborators",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X DELETE -d '{\"email\":\"old_guy@strider-cd.com\"}' http://localhost/api/strider-cd/strider/collaborators",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "email",
"description": "<p>Email address to remove from the repo/project.</p>"
}
],
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
},
"filename": "lib/routes/collaborators/index.js",
"groupTitle": "Collaborators"
},
{
"type": "get",
"url": "/:org/:repo/collaborators",
"title": "Get Collaborators",
"description": "<p>Gets a list of collaborators for a project</p>",
"name": "GetCollaborators",
"group": "Collaborators",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X GET http://localhost/api/strider-cd/strider/collaborators",
"type": "curl"
}
],
"filename": "lib/routes/collaborators/index.js",
"groupTitle": "Collaborators",
"parameter": {
"fields": {
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
}
},
{
"type": "get",
"url": "/api/jobs",
"title": "Get Latest Jobs",
"description": "<p>Return JSON object containing the most recent build status for each configured repo This function is used to build the main dashboard status page. The result is separated into <code>{public: [], yours: []}</code>.</p> <p>Note: the private ones are just ones that the current user is a collaborator on and are not necessarily private</p>",
"name": "GetJobs",
"group": "Job",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X GET http://localhost/api/jobs",
"type": "curl"
}
],
"filename": "lib/routes/api/jobs.js",
"groupTitle": "Job"
},
{
"type": "post",
"url": "/:org/:repo/start",
"title": "Start Job",
"description": "<p>Executes a strider test and, optionally, deployment.</p>",
"name": "StartJob",
"group": "Job",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X POST http://localhost/api/strider-cd/strider/start",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "type",
"defaultValue": "TEST_ONLY",
"description": "<p>Denotes the type of job to run. This can be "TEST_ONLY", which indicates that only the test stages of the job should be executed or "TEST_AND_DEPLOY", which indicates that all stages should be executed.</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "branch",
"defaultValue": "master",
"description": "<p>Indicates which branch configuration should be executed.</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "message",
"defaultValue": "Manually Retesting/Redeploying",
"description": "<p>An optional message to include as the title of the execution.</p>"
}
],
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
},
"filename": "lib/routes/api/jobs.js",
"groupTitle": "Job"
},
{
"type": "get",
"url": "/:org/:repo/provider",
"title": "Get Project Provider",
"description": "<p>Get the provider config for the specified project</p>",
"name": "GetProjectProvider",
"group": "Provider",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X GET http://localhost:3000/strider-cd/strider/provider",
"type": "curl"
}
],
"filename": "lib/routes/provider.js",
"groupTitle": "Provider",
"parameter": {
"fields": {
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
}
},
{
"type": "post",
"url": "/:org/:repo/provider",
"title": "Update Project Provider",
"description": "<p>Update a project's provider</p>",
"name": "UpdateProjectProvider",
"group": "Provider",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X POST http://localhost:3000/strider-cd/strider/provider",
"type": "curl"
}
],
"filename": "lib/routes/provider.js",
"groupTitle": "Provider",
"parameter": {
"fields": {
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
}
},
{
"type": "delete",
"url": "/:org/:repo/cache",
"title": "Clear Cache",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Clears/invalidates the cache for a project.</p>",
"name": "ClearCache",
"group": "Repo",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X DELETE http://localhost/api/strider-cd/strider/cache",
"type": "curl"
}
],
"filename": "lib/routes/api/repo.js",
"groupTitle": "Repo",
"parameter": {
"fields": {
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
}
},
{
"type": "put",
"url": "/:org",
"title": "Create Repo",
"description": "<p>Create a new project for a repo.</p>",
"name": "CreateRepo",
"group": "Repo",
"version": "1.0.0",
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "name",
"description": "<p>The name of the new branch</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "display_name",
"description": "<p>Human-readable project name</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "display_url",
"description": "<p>The URL for the repo (e.g. Github homepage)</p>"
},
{
"group": "RequestBody",
"type": "Boolean",
"optional": false,
"field": "public",
"defaultValue": "false",
"description": "<p>Whether this project is public or not.</p>"
},
{
"group": "RequestBody",
"type": "Boolean",
"optional": false,
"field": "prefetch_config",
"defaultValue": "true",
"description": "<p>Whether the strider.json should be fetched in advance.</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "account",
"description": "<p>The ID of provider account</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "repo_id",
"description": "<p>The ID of the repo</p>"
},
{
"group": "RequestBody",
"type": "Object",
"optional": false,
"field": "provider",
"description": "<p>A json object with 'id' and 'config' properties.</p>"
}
]
}
},
"filename": "lib/routes/api/repo.js",
"groupTitle": "Repo"
},
{
"type": "delete",
"url": "/:org/:repo",
"title": "Delete Repo",
"permission": [
{
"name": "ProjectAdmin",
"title": "You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.",
"description": ""
}
],
"description": "<p>Deletes a repository/project. Also archives all jobs (marks as archived in DB which makes them hidden).</p>",
"name": "DeleteRepo",
"group": "Repo",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X DELETE http://localhost/api/strider-cd/strider",
"type": "curl"
}
],
"filename": "lib/routes/api/repo.js",
"groupTitle": "Repo",
"parameter": {
"fields": {
"Request URL Parameters": [
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "org",
"description": "<p>The organization name for the project. This is usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider") but may vary from one project provider to another. (as another example, in GitLab this refers to the repository's "group").</p>"
},
{
"group": "RequestUrl",
"type": "String",
"optional": false,
"field": "repo",
"description": "<p>The project's repository name.</p>"
}
]
}
}
},
{
"type": "post",
"url": "/api/session",
"title": "Create New Session",
"description": "<p>Creates a new user session after validating an email address and password pair.</p>",
"name": "CreateSession",
"group": "Session",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X POST -d email=me@me.com -d password=mypass http://localhost/api/session",
"type": "curl"
}
],
"parameter": {
"fields": {
"Request Body Parameters": [
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "email",
"description": "<p>The email address to login as (which is used as the username).</p>"
},
{
"group": "RequestBody",
"type": "String",
"optional": false,
"field": "password",
"description": "<p>The user's password.</p>"
}
]
}
},
"filename": "lib/routes/api/session.js",
"groupTitle": "Session"
},
{
"type": "get",
"url": "/api/session",
"title": "Get Session",
"description": "<p>Gets the current session information</p>",
"name": "GetSession",
"group": "Session",
"version": "1.0.0",
"examples": [
{
"title": "CURL Example:",
"content": "curl -X GET http://localhost/api/session",
"type": "curl"
}
],
"filename": "lib/routes/api/session.js",
"groupTitle": "Session"
}
] });
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 2 | define({
"name": "strider",
"version": "1.0.0",
"description": "Strider API",
"apidoc": "0.2.0",
"sampleUrl": false,
"generator": {
"name": "apidoc",
"time": "2017-02-18T17:20:45.844Z",
"url": "http://apidocjs.com",
"version": "0.16.1"
}
});
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| ca.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| de.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| es.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| fr.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| it.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| locale.js | 15% | (3 / 20) | 0% | (0 / 12) | 0% | (0 / 3) | 15% | (3 / 20) | |
| nl.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| pl.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| pt_br.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| ru.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| zh.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| zh_cn.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
ca: {
'Allowed values:' : 'Valors permesos:',
'Compare all with predecessor': 'Comparar tot amb versió anterior',
'compare changes to:' : 'comparar canvis amb:',
'compared to' : 'comparat amb',
'Default value:' : 'Valor per defecte:',
'Description' : 'Descripció',
'Field' : 'Camp',
'General' : 'General',
'Generated with' : 'Generat amb',
'Name' : 'Nom',
'No response values.' : 'Sense valors en la resposta.',
'optional' : 'opcional',
'Parameter' : 'Paràmetre',
'Permission:' : 'Permisos:',
'Response' : 'Resposta',
'Send' : 'Enviar',
'Send a Sample Request' : 'Enviar una petició d\'exemple',
'show up to version:' : 'mostrar versió:',
'Size range:' : 'Tamany de rang:',
'Type' : 'Tipus',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
de: {
'Allowed values:' : 'Erlaubte Werte:',
'Compare all with predecessor': 'Vergleiche alle mit ihren Vorgängern',
'compare changes to:' : 'vergleiche Änderungen mit:',
'compared to' : 'verglichen mit',
'Default value:' : 'Standardwert:',
'Description' : 'Beschreibung',
'Field' : 'Feld',
'General' : 'Allgemein',
'Generated with' : 'Erstellt mit',
'Name' : 'Name',
'No response values.' : 'Keine Rückgabewerte.',
'optional' : 'optional',
'Parameter' : 'Parameter',
'Permission:' : 'Berechtigung:',
'Response' : 'Antwort',
'Send' : 'Senden',
'Send a Sample Request' : 'Eine Beispielanfrage senden',
'show up to version:' : 'zeige bis zur Version:',
'Size range:' : 'Größenbereich:',
'Type' : 'Typ',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
es: {
'Allowed values:' : 'Valores permitidos:',
'Compare all with predecessor': 'Comparar todo con versión anterior',
'compare changes to:' : 'comparar cambios con:',
'compared to' : 'comparado con',
'Default value:' : 'Valor por defecto:',
'Description' : 'Descripción',
'Field' : 'Campo',
'General' : 'General',
'Generated with' : 'Generado con',
'Name' : 'Nombre',
'No response values.' : 'Sin valores en la respuesta.',
'optional' : 'opcional',
'Parameter' : 'Parámetro',
'Permission:' : 'Permisos:',
'Response' : 'Respuesta',
'Send' : 'Enviar',
'Send a Sample Request' : 'Enviar una petición de ejemplo',
'show up to version:' : 'mostrar a versión:',
'Size range:' : 'Tamaño de rango:',
'Type' : 'Tipo',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
fr: {
'Allowed values:' : 'Valeurs autorisées :',
'Compare all with predecessor': 'Tout comparer avec ...',
'compare changes to:' : 'comparer les changements à :',
'compared to' : 'comparer à',
'Default value:' : 'Valeur par défaut :',
'Description' : 'Description',
'Field' : 'Champ',
'General' : 'Général',
'Generated with' : 'Généré avec',
'Name' : 'Nom',
'No response values.' : 'Aucune valeur de réponse.',
'optional' : 'optionnel',
'Parameter' : 'Paramètre',
'Permission:' : 'Permission :',
'Response' : 'Réponse',
'Send' : 'Envoyer',
'Send a Sample Request' : 'Envoyer une requête représentative',
'show up to version:' : 'Montrer à partir de la version :',
'Size range:' : 'Ordre de grandeur :',
'Type' : 'Type',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
it: {
'Allowed values:' : 'Valori permessi:',
'Compare all with predecessor': 'Confronta tutto con versioni precedenti',
'compare changes to:' : 'confronta modifiche con:',
'compared to' : 'confrontato con',
'Default value:' : 'Valore predefinito:',
'Description' : 'Descrizione',
'Field' : 'Campo',
'General' : 'Generale',
'Generated with' : 'Creato con',
'Name' : 'Nome',
'No response values.' : 'Nessnu valore di risposta.',
'optional' : 'opzionale',
'Parameter' : 'Parametro',
'Permission:' : 'Permessi:',
'Response' : 'Risposta',
'Send' : 'Invia',
'Send a Sample Request' : 'Invia una richiesta di esempio',
'show up to version:' : 'visualizza la versione:',
'Size range:' : 'Intervallo dimensione:',
'Type' : 'Tipo',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 2 1 1 | define([
'./locales/ca.js',
'./locales/de.js',
'./locales/es.js',
'./locales/fr.js',
'./locales/it.js',
'./locales/nl.js',
'./locales/pl.js',
'./locales/pt_br.js',
'./locales/ru.js',
'./locales/zh.js',
'./locales/zh_cn.js'
], function() {
var langId = (navigator.language || navigator.userLanguage).toLowerCase().replace('-', '_');
var language = langId.substr(0, 2);
var locales = {};
for (index in arguments) {
for (property in arguments[index])
locales[property] = arguments[index][property];
}
if ( ! locales['en'])
locales['en'] = {};
if ( ! locales[langId] && ! locales[language])
language = 'en';
var locale = (locales[langId] ? locales[langId] : locales[language]);
function __(text) {
var index = locale[text];
if (index === undefined)
return text;
return index;
};
function setLanguage(language) {
locale = locales[language];
}
return {
__ : __,
locales : locales,
locale : locale,
setLanguage: setLanguage
};
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
nl: {
'Allowed values:' : 'Toegestane waarden:',
'Compare all with predecessor': 'Vergelijk alle met voorgaande versie',
'compare changes to:' : 'vergelijk veranderingen met:',
'compared to' : 'vergelijk met',
'Default value:' : 'Standaard waarde:',
'Description' : 'Omschrijving',
'Field' : 'Veld',
'General' : 'Algemeen',
'Generated with' : 'Gegenereerd met',
'Name' : 'Naam',
'No response values.' : 'Geen response waardes.',
'optional' : 'optioneel',
'Parameter' : 'Parameter',
'Permission:' : 'Permissie:',
'Response' : 'Antwoorden',
'Send' : 'Sturen',
'Send a Sample Request' : 'Stuur een sample aanvragen',
'show up to version:' : 'toon tot en met versie:',
'Size range:' : 'Maatbereik:',
'Type' : 'Type',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
pl: {
'Allowed values:' : 'Dozwolone wartości:',
'Compare all with predecessor': 'Porównaj z poprzednimi wersjami',
'compare changes to:' : 'porównaj zmiany do:',
'compared to' : 'porównaj do:',
'Default value:' : 'Wartość domyślna:',
'Description' : 'Opis',
'Field' : 'Pole',
'General' : 'Generalnie',
'Generated with' : 'Wygenerowano z',
'Name' : 'Nazwa',
'No response values.' : 'Brak odpowiedzi.',
'optional' : 'opcjonalny',
'Parameter' : 'Parametr',
'Permission:' : 'Uprawnienia:',
'Response' : 'Odpowiedź',
'Send' : 'Wyślij',
'Send a Sample Request' : 'Wyślij przykładowe żądanie',
'show up to version:' : 'pokaż do wersji:',
'Size range:' : 'Zakres rozmiaru:',
'Type' : 'Typ',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
'pt_br': {
'Allowed values:' : 'Valores permitidos:',
'Compare all with predecessor': 'Compare todos com antecessores',
'compare changes to:' : 'comparar alterações com:',
'compared to' : 'comparado com',
'Default value:' : 'Valor padrão:',
'Description' : 'Descrição',
'Field' : 'Campo',
'General' : 'Geral',
'Generated with' : 'Gerado com',
'Name' : 'Nome',
'No response values.' : 'Sem valores de resposta.',
'optional' : 'opcional',
'Parameter' : 'Parâmetro',
'Permission:' : 'Permissão:',
'Response' : 'Resposta',
'Send' : 'Enviar',
'Send a Sample Request' : 'Enviar um Exemplo de Pedido',
'show up to version:' : 'aparecer para a versão:',
'Size range:' : 'Faixa de tamanho:',
'Type' : 'Tipo',
'url' : 'url'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
ru: {
'Allowed values:' : 'Допустимые значения:',
'Compare all with predecessor': 'Сравнить с предыдущей версией',
'compare changes to:' : 'сравнить с:',
'compared to' : 'в сравнении с',
'Default value:' : 'По умолчанию:',
'Description' : 'Описание',
'Field' : 'Название',
'General' : 'Общая информация',
'Generated with' : 'Сгенерировано с помощью',
'Name' : 'Название',
'No response values.' : 'Нет значений для ответа.',
'optional' : 'необязательный',
'Parameter' : 'Параметр',
'Permission:' : 'Разрешено:',
'Response' : 'Ответ',
'Send' : 'Отправить',
'Send a Sample Request' : 'Отправить тестовый запрос',
'show up to version:' : 'показать версию:',
'Size range:' : 'Ограничения:',
'Type' : 'Тип',
'url' : 'URL'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
zh: {
'Allowed values:' : '允許值:',
'Compare all with predecessor': '預先比較所有',
'compare changes to:' : '比較變更:',
'compared to' : '對比',
'Default value:' : '默認值:',
'Description' : '描述',
'Field' : '字段',
'General' : '概括',
'Generated with' : '生成工具',
'Name' : '名稱',
'No response values.' : '無對應資料.',
'optional' : '選項',
'Parameter' : '參數',
'Permission:' : '允許:',
'Response' : '回應',
'Send' : '發送',
'Send a Sample Request' : '發送試用需求',
'show up to version:' : '顯示到版本:',
'Size range:' : '尺寸範圍:',
'Type' : '類型',
'url' : '網址'
}
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | define({
'zh_cn': {
'Allowed values:' : '允许值:',
'Compare all with predecessor': '与所有较早的比较',
'compare changes to:' : '将当前版本与指定版本比较:',
'compared to' : '相比于',
'Default value:' : '默认值:',
'Description' : '描述',
'Field' : '字段',
'General' : '概要',
'Generated with' : '基于',
'Name' : '名称',
'No response values.' : '无返回值.',
'optional' : '可选',
'Parameter' : '参数',
'Permission:' : '权限:',
'Response' : '返回',
'Send' : '发送',
'Send a Sample Request' : '发送示例请求',
'show up to version:' : '显示到指定版本:',
'Size range:' : '取值范围:',
'Type' : '类型',
'url' : '网址'
}
});
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| handlebars_helper.js | 1.81% | (3 / 166) | 0% | (0 / 64) | 0% | (0 / 31) | 1.83% | (3 / 164) | |
| send_sample_request.js | 7.22% | (7 / 97) | 0% | (0 / 18) | 0% | (0 / 17) | 7.22% | (7 / 97) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | 2 1 1 | define([
'locales',
'handlebars',
'diffMatchPatch'
], function(locale, Handlebars, DiffMatchPatch) {
/**
* start/stop timer for simple performance check.
*/
var timer;
Handlebars.registerHelper('startTimer', function(text) {
timer = new Date();
return '';
});
Handlebars.registerHelper('stopTimer', function(text) {
console.log(new Date() - timer);
return '';
});
/**
* Return localized Text.
* @param string text
*/
Handlebars.registerHelper('__', function(text) {
return locale.__(text);
});
/**
* Console log.
* @param mixed obj
*/
Handlebars.registerHelper('cl', function(obj) {
console.log(obj);
return '';
});
/**
* Replace underscore with space.
* @param string text
*/
Handlebars.registerHelper('underscoreToSpace', function(text) {
return text.replace(/(_+)/g, ' ');
});
/**
*
*/
Handlebars.registerHelper('assign', function(name) {
if(arguments.length > 0) {
var type = typeof(arguments[1]);
var arg = null;
if(type === 'string' || type === 'number' || type === 'boolean') arg = arguments[1];
Handlebars.registerHelper(name, function() { return arg; });
}
return '';
});
/**
*
*/
Handlebars.registerHelper('nl2br', function(text) {
return _handlebarsNewlineToBreak(text);
});
/**
*
*/
Handlebars.registerHelper('if_eq', function(context, options) {
var compare = context;
// Get length if context is an object
if (context instanceof Object && ! (options.hash.compare instanceof Object))
compare = Object.keys(context).length;
if (compare === options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
*
*/
Handlebars.registerHelper('if_gt', function(context, options) {
var compare = context;
// Get length if context is an object
if (context instanceof Object && ! (options.hash.compare instanceof Object))
compare = Object.keys(context).length;
if(compare > options.hash.compare)
return options.fn(this);
return options.inverse(this);
});
/**
*
*/
var templateCache = {};
Handlebars.registerHelper('subTemplate', function(name, sourceContext) {
if ( ! templateCache[name])
templateCache[name] = Handlebars.compile($('#template-' + name).html());
var template = templateCache[name];
var templateContext = $.extend({}, this, sourceContext.hash);
return new Handlebars.SafeString( template(templateContext) );
});
/**
*
*/
Handlebars.registerHelper('toLowerCase', function(value) {
return (value && typeof value === 'string') ? value.toLowerCase() : '';
});
/**
*
*/
Handlebars.registerHelper('splitFill', function(value, splitChar, fillChar) {
var splits = value.split(splitChar);
return new Array(splits.length).join(fillChar) + splits[splits.length - 1];
});
/**
* Convert Newline to HTML-Break (nl2br).
*
* @param {String} text
* @returns {String}
*/
function _handlebarsNewlineToBreak(text) {
return ('' + text).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
}
/**
*
*/
Handlebars.registerHelper('each_compare_list_field', function(source, compare, options) {
var fieldName = options.hash.field;
var newSource = [];
if (source) {
source.forEach(function(entry) {
var values = entry;
values['key'] = entry[fieldName];
newSource.push(values);
});
}
var newCompare = [];
if (compare) {
compare.forEach(function(entry) {
var values = entry;
values['key'] = entry[fieldName];
newCompare.push(values);
});
}
return _handlebarsEachCompared('key', newSource, newCompare, options);
});
/**
*
*/
Handlebars.registerHelper('each_compare_keys', function(source, compare, options) {
var newSource = [];
if (source) {
var sourceFields = Object.keys(source);
sourceFields.forEach(function(name) {
var values = {};
values['value'] = source[name];
values['key'] = name;
newSource.push(values);
});
}
var newCompare = [];
if (compare) {
var compareFields = Object.keys(compare);
compareFields.forEach(function(name) {
var values = {};
values['value'] = compare[name];
values['key'] = name;
newCompare.push(values);
});
}
return _handlebarsEachCompared('key', newSource, newCompare, options);
});
/**
*
*/
Handlebars.registerHelper('each_compare_field', function(source, compare, options) {
return _handlebarsEachCompared('field', source, compare, options);
});
/**
*
*/
Handlebars.registerHelper('each_compare_title', function(source, compare, options) {
return _handlebarsEachCompared('title', source, compare, options);
});
/**
*
*/
Handlebars.registerHelper('reformat', function(source, type){
if (type == 'json')
try {
return JSON.stringify(JSON.parse(source.trim()),null, " ");
} catch(e) {
}
return source
});
/**
*
*/
Handlebars.registerHelper('showDiff', function(source, compare, options) {
var ds = '';
if(source === compare) {
ds = source;
} else {
if( ! source)
return compare;
if( ! compare)
return source;
var d = diffMatchPatch.diff_main(compare, source);
diffMatchPatch.diff_cleanupSemantic(d);
ds = diffMatchPatch.diff_prettyHtml(d);
ds = ds.replace(/¶/gm, '');
}
if(options === 'nl2br')
ds = _handlebarsNewlineToBreak(ds);
return ds;
});
/**
*
*/
function _handlebarsEachCompared(fieldname, source, compare, options)
{
var dataList = [];
var index = 0;
if(source) {
source.forEach(function(sourceEntry) {
var found = false;
if (compare) {
compare.forEach(function(compareEntry) {
if(sourceEntry[fieldname] === compareEntry[fieldname]) {
var data = {
typeSame: true,
source: sourceEntry,
compare: compareEntry,
index: index
};
dataList.push(data);
found = true;
index++;
}
});
}
if ( ! found) {
var data = {
typeIns: true,
source: sourceEntry,
index: index
};
dataList.push(data);
index++;
}
});
}
if (compare) {
compare.forEach(function(compareEntry) {
var found = false;
if (source) {
source.forEach(function(sourceEntry) {
if(sourceEntry[fieldname] === compareEntry[fieldname])
found = true;
});
}
if ( ! found) {
var data = {
typeDel: true,
compare: compareEntry,
index: index
};
dataList.push(data);
index++;
}
});
}
var ret = '';
var length = dataList.length;
for (var index in dataList) {
if(index == (length - 1))
dataList[index]['_last'] = true;
ret = ret + options.fn(dataList[index]);
}
return ret;
}
var diffMatchPatch = new DiffMatchPatch();
/**
* Overwrite Colors
*/
DiffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
var html = [];
var pattern_amp = /&/g;
var pattern_lt = /</g;
var pattern_gt = />/g;
var pattern_para = /\n/g;
for (var x = 0; x < diffs.length; x++) {
var op = diffs[x][0]; // Operation (insert, delete, equal)
var data = diffs[x][1]; // Text of change.
var text = data.replace(pattern_amp, '&').replace(pattern_lt, '<')
.replace(pattern_gt, '>').replace(pattern_para, '¶<br>');
switch (op) {
case DIFF_INSERT:
html[x] = '<ins>' + text + '</ins>';
break;
case DIFF_DELETE:
html[x] = '<del>' + text + '</del>';
break;
case DIFF_EQUAL:
html[x] = '<span>' + text + '</span>';
break;
}
}
return html.join('');
};
// Exports
return Handlebars;
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 2 1 1 1 1 1 1 | define([ 'jquery' ], function($) { var initDynamic = function() { // Button send $(".sample-request-send").off("click"); $(".sample-request-send").on("click", function(e) { e.preventDefault(); var $root = $(this).parents("article"); var group = $root.data("group"); var name = $root.data("name"); var version = $root.data("version"); sendSampleRequest(group, name, version, $(this).data("sample-request-type")); }); // Button clear $(".sample-request-clear").off("click"); $(".sample-request-clear").on("click", function(e) { e.preventDefault(); var $root = $(this).parents("article"); var group = $root.data("group"); var name = $root.data("name"); var version = $root.data("version"); clearSampleRequest(group, name, version); }); }; // initDynamic function sendSampleRequest(group, name, version, type) { var $root = $('article[data-group="' + group + '"][data-name="' + name + '"][data-version="' + version + '"]'); // Optional header var header = {}; $root.find(".sample-request-header:checked").each(function(i, element) { var group = $(element).data("sample-request-header-group-id"); $root.find("[data-sample-request-header-group=\"" + group + "\"]").each(function(i, element) { var key = $(element).data("sample-request-header-name"); var value = element.value; if ( ! element.optional && element.defaultValue !== '') { value = element.defaultValue; } header[key] = value; }); }); // create JSON dictionary of parameters var param = {}; var paramType = {}; $root.find(".sample-request-param:checked").each(function(i, element) { var group = $(element).data("sample-request-param-group-id"); $root.find("[data-sample-request-param-group=\"" + group + "\"]").each(function(i, element) { var key = $(element).data("sample-request-param-name"); var value = element.value; if ( ! element.optional && element.defaultValue !== '') { value = element.defaultValue; } param[key] = value; paramType[key] = $(element).next().text(); }); }); // grab user-inputted URL var url = $root.find(".sample-request-url").val(); // Insert url parameter var pattern = pathToRegexp(url, null); var matches = pattern.exec(url); for (var i = 1; i < matches.length; i++) { var key = matches[i].substr(1); if (param[key] !== undefined) { url = url.replace(matches[i], encodeURIComponent(param[key])); // remove URL parameters from list delete param[key]; } } // for $root.find(".sample-request-response").fadeTo(250, 1); $root.find(".sample-request-response-json").html("Loading..."); refreshScrollSpy(); _.each( param, function( val, key ) { var t = paramType[ key ].toLowerCase(); if ( t === 'object' || t === 'array' ) { try { param[ key ] = JSON.parse( val ); } catch (e) { } } }); // send AJAX request, catch success or error callback var ajaxRequest = { url : url, headers : header, data : param, type : type.toUpperCase(), success : displaySuccess, error : displayError }; $.ajax(ajaxRequest); function displaySuccess(data, status, jqXHR) { var jsonResponse; try { jsonResponse = JSON.parse(jqXHR.responseText); jsonResponse = JSON.stringify(jsonResponse, null, 4); } catch (e) { jsonResponse = data; } $root.find(".sample-request-response-json").html(jsonResponse); refreshScrollSpy(); }; function displayError(jqXHR, textStatus, error) { var message = "Error " + jqXHR.status + ": " + error; var jsonResponse; try { jsonResponse = JSON.parse(jqXHR.responseText); jsonResponse = JSON.stringify(jsonResponse, null, 4); } catch (e) { jsonResponse = escape(jqXHR.responseText); } if (jsonResponse) message += "<br>" + jsonResponse; // flicker on previous error to make clear that there is a new response if($root.find(".sample-request-response").is(":visible")) $root.find(".sample-request-response").fadeTo(1, 0.1); $root.find(".sample-request-response").fadeTo(250, 1); $root.find(".sample-request-response-json").html(message); refreshScrollSpy(); }; } function clearSampleRequest(group, name, version) { var $root = $('article[data-group="' + group + '"][data-name="' + name + '"][data-version="' + version + '"]'); // hide sample response $root.find(".sample-request-response-json").html(""); $root.find(".sample-request-response").hide(); // reset value of parameters $root.find(".sample-request-param").each(function(i, element) { element.value = ""; }); // restore default URL var $urlElement = $root.find(".sample-request-url"); $urlElement.val($urlElement.prop("defaultValue")); refreshScrollSpy(); } function refreshScrollSpy() { $('[data-spy="scroll"]').each(function () { $(this).scrollspy("refresh"); }); } function escapeHtml(str) { var div = document.createElement("div"); div.appendChild(document.createTextNode(str)); return div.innerHTML; } /** * Exports. */ return { initDynamic: initDynamic }; }); |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| app.js | 3.85% | (1 / 26) | 100% | (0 / 0) | 100% | (0 / 0) | 3.85% | (1 / 26) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 2 | 'use strict';
require('bootstrap');
var $ = require('jquery');
var _ = require('lodash');
var angular = require('angular');
var $navbar = $('.navbar');
require('angular-route');
$navbar.find('li').removeClass('active');
$navbar.find(`a[href="${global.location.pathname}"]`)
.parent().addClass('active');
$('#layout-header').hide();
$('#invite-box').height($('#signup-box').height());
// Third party
require('ui-bootstrap');
require('ui-codemirror');
// Modules
require('./account');
require('./config');
require('./plugin-manager');
require('./job-status');
require('./dashboard');
require('./projects');
// Shared?
require('./alerts');
require('./ansi');
require('./moment');
var app = angular.module('app', [
'config',
'account',
'plugin-manager',
'job-status',
'dashboard',
'projects'
]);
// For access from plugins, need a better way
global.app = app;
global.$ = $;
global.angular = angular;
global._ = _;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| interpolate.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 0% | (0 / 1) | 33.33% | (1 / 3) | |
| job-data-monitor.js | 14.29% | (8 / 56) | 0% | (0 / 14) | 0% | (0 / 8) | 15.09% | (8 / 53) | |
| job-monitor.js | 6.49% | (5 / 77) | 0% | (0 / 36) | 0% | (0 / 17) | 6.94% | (5 / 72) | |
| ng-sortable-directive.js | 5.66% | (3 / 53) | 0% | (0 / 28) | 0% | (0 / 7) | 6.38% | (3 / 47) | |
| phases.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| post.js | 22.22% | (2 / 9) | 0% | (0 / 4) | 0% | (0 / 3) | 22.22% | (2 / 9) | |
| skels.js | 100% | (3 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (3 / 3) | |
| status-classes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 | 1 | 'use strict'; module.exports = function ($interpolateProvider) { $interpolateProvider.startSymbol('[['); $interpolateProvider.endSymbol(']]'); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var JobMonitor = require('./job-monitor');
var SKELS = require('./skels');
function JobDataMonitor() {
JobMonitor.apply(this, arguments);
}
_.extend(JobDataMonitor.prototype, JobMonitor.prototype, {});
JobDataMonitor.prototype.statuses = _.extend({}, JobMonitor.prototype.statuses, {
'phase.done': function (data) {
this.phases[data.phase].finished = data.time;
this.phases[data.phase].duration = data.elapsed;
this.phases[data.phase].exitCode = data.code;
if (['prepare', 'environment', 'cleanup'].indexOf(data.phase) !== -1) {
this.phases[data.phase].collapsed = true;
}
if (data.phase === 'test') this.test_status = data.code;
if (data.phase === 'deploy') this.deploy_status = data.code;
if (!data.next || !this.phases[data.next]) return;
this.phase = data.next;
this.phases[data.next].started = data.time;
},
'command.comment': function (data) {
var phase = this.phases[this.phase];
var command = _.extend({}, SKELS.command);
command.command = data.comment;
command.comment = true;
command.plugin = data.plugin;
command.finished = data.time;
phase.commands.push(command);
},
'command.start': function (data) {
var phase = this.phases[this.phase];
var command = _.extend({}, SKELS.command, data);
command.started = data.time;
phase.commands.push(command);
},
'command.done': function (data) {
var phase = this.phases[this.phase];
var command = phase.commands[phase.commands.length - 1];
command.finished = data.time;
command.duration = data.elapsed;
command.exitCode = data.exitCode;
command.merged = command._merged;
},
'stdout': function (text) {
var command = ensureCommand(this.phases[this.phase]);
command.out += text;
command._merged += text;
this.std.out += text;
this.std.merged += text;
this.std.merged_latest = text;
},
'stderr': function (text) {
var command = ensureCommand(this.phases[this.phase]);
command.err += text;
command._merged += text;
this.std.err += text;
this.std.merged += text;
this.std.merged_latest = text;
}
});
function ensureCommand(phase) {
var command = phase.commands[phase.commands.length - 1];
if (!command || typeof(command.finished) !== 'undefined') {
command = _.extend({}, SKELS.command);
phase.commands.push(command);
}
return command;
}
module.exports = JobDataMonitor;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var PHASES = require('./phases');
// The Job Monitor:
// - update jobs based on browser events
//
function JobMonitor(socket, changed) {
this.sock = socket;
this.changed = changed;
this.waiting = {};
this.listen();
}
JobMonitor.prototype = {
emits: {
getUnknown: 'dashboard:unknown'
},
events: {
'job.new': function (job, access) {
this.addJob(job[0], access);
this.changed();
},
'job.done': function (job, access) {
this.addJob(job[0], access);
this.changed();
}
},
job: function () {
throw new Error('You must override this');
},
addJob: function () {
throw new Error('You must implement');
},
listen: function () {
var handler;
for (var event in this.events) {
handler = this.events[event];
if ('string' === typeof handler) handler = this[handler];
this.sock.on(event, handler.bind(this));
}
for (var status in this.statuses) {
this.sock.on(`job.status.${status}`, this.update.bind(this, status));
}
},
// access: 'yours', 'public', 'admin'
update: function (event, args, access, dontchange) {
var id = args.shift();
var job = this.job(id, access);
var handler = this.statuses[event];
if (!job) return this.unknown(id, event, args, access);
if (!handler) return;
if ('string' === typeof handler) {
job.status = handler;
} else {
handler.apply(job, args);
}
if (!dontchange) this.changed();
},
unknown: function (id, event, args, access) {
args = [id].concat(args);
if (this.waiting[id]) {
return this.waiting[id].push([event, args, access]);
}
this.waiting[id] = [[event, args, access]];
this.sock.emit(this.emits.getUnknown, id, this.gotUnknown.bind(this));
},
gotUnknown: function (job) {
if (!this.waiting[job._id]) return console.warn('Got unknownjob:response but wan\'t waiting for it...');
var access = this.waiting[job._id][0][2];
if (job.status === 'submitted') {
job.status = 'running';
job.started = new Date();
}
// job.phase = job.phase || 'environment';
this.addJob(job, access);
// TODO: this.update searches for the job again. optimize
for (var i = 0; i < this.waiting[job._id]; i++) {
this.update.apply(this, this.waiting[i].concat([true]));
}
delete this.waiting[job._id];
this.changed();
},
statuses: {
'started': function (time) {
this.started = time;
this.phase = 'environment';
this.status = 'running';
},
'errored': function (error) {
this.error = error;
this.status = 'errored';
},
'canceled': 'errored',
'phase.done': function (data) {
this.phase = PHASES.indexOf(data.phase) + 1;
},
// this is just so we'll trigger the "unknown job" lookup sooner on the dashboard
'stdout': function () {
},
'stderr': function () {
},
'warning': function (warning) {
if (!this.warnings) {
this.warnings = [];
}
this.warnings.push(warning);
},
'plugin-data': function (data) {
var path = data.path ? [data.plugin].concat(data.path.split('.')) : [data.plugin];
var last = path.pop();
var method = data.method || 'replace';
var parent;
parent = path.reduce(function (obj, attr) {
return obj[attr] || (obj[attr] = {});
}, this.plugin_data || (this.plugin_data = {}));
if (method === 'replace') {
parent[last] = data.data;
} else if (method === 'push') {
if (!parent[last]) {
parent[last] = [];
}
parent[last].push(data.data);
} else if (method === 'extend') {
if (!parent[last]) {
parent[last] = {};
}
_.extend(parent[last], data.data);
} else {
console.error('Invalid "plugin data" method received from plugin', data.plugin, data.method, data);
}
}
}
};
module.exports = JobMonitor;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 2 2 1 | 'use strict';
var _ = require('lodash');
var $ = require('jquery');
var Sortable = require('sortable');
module.exports = function ($parse) {
return {
compile: function ($element, attr) {
var opts = {};
var onAdd = null;
var onRemove = null;
var groupName = attr['ngSortableGroup'];
var onUpdate = attr['ngSortable'] || attr['ngSortableUpdate'];
var dataSource = attr['ngSortableSource'];
var model = attr['ngModel'];
if (groupName) {
if (!dataSource)
throw new Error('Use of a group requires specifying a data source via ng-sortable-source');
onAdd = attr['ngSortableAdded'];
onRemove = attr['ngSortableRemoved'];
}
var key = attr['ngSortableKey'] || '_id';
return function (scope, element) {
function bind(fnStr) {
var fn = $parse(fnStr)(scope);
return function (event) {
scope.$apply(function () {
var data = event.type === 'update' ? model : dataSource;
var list = _.cloneDeep($parse(data)(scope));
var $el = $(event.target);
var id = $($el).attr('ng-sortable-id');
var oldIndex = null;
var newIndex = $el.index();
if (!id) throw new Error('No ng-sortable-id on element.');
var target = _.find(list, function (b, i) {
oldIndex = i;
return b[key] === id;
});
if (!target) {
// this requirement can go away if we set IDs ourself during compiletime
// but generally you'll have an ID to give
throw new Error('Could not locate target element. Did you forget to set attribute data-ng-sortable-id on your repeated HTML elements?');
}
if (event.type === 'update') {
list.splice(oldIndex, 1);
list.splice(newIndex, 0, target);
fn(list);
} else if (event.type === 'add') {
fn(target, newIndex, event);
}
});
};
}
if (onUpdate) opts.onUpdate = bind(onUpdate);
if (groupName) opts.group = groupName;
if (onAdd) opts.onAdd = bind(onAdd);
if (onRemove) opts.onRemove = bind(onRemove);
if (onUpdate) opts.onUpdate = bind(onUpdate);
scope.sortable = new Sortable(element.get(0), opts);
};
}
};
};
|
| 1 2 3 4 5 | 1 | 'use strict'; module.exports = ['environment', 'prepare', 'test', 'deploy', 'cleanup']; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 2 1 | 'use strict';
var $ = require('jquery');
function post(url, data, done) {
$.ajax({
url: url,
type: 'POST',
data: data,
dataType: 'json',
success: function () {
done(null);
},
error: function (xhr, ts, e) {
if (xhr && xhr.responseText) {
var data = $.parseJSON(xhr.responseText);
e = data.errors[0];
}
done(e);
}
});
}
module.exports = post;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 1 1 1 | 'use strict';
var PHASES = require('./phases');
var SKELS = {
job: {
id: null,
data: null,
phases: {},
phase: PHASES[0],
queued: null,
started: null,
finished: null,
test_status: null,
deploy_status: null,
plugin_data: {},
warnings: [],
std: {
out: '',
err: '',
merged: '',
merged_latest: ''
}
},
command: {
out: '',
err: '',
merged: '',
_merged: '',
started: null,
command: '',
plugin: ''
},
phase: {
finished: null,
exitCode: null,
commands: []
}
};
module.exports = SKELS;
|
| 1 2 3 4 5 6 7 8 9 10 11 | 1 | 'use strict';
module.exports = {
passed: 'fa-check-circle success-text',
failed: 'fa-exclamation-circle failure-text',
running: 'fa-circle-o-notch fa-spin running-text',
submitted: 'fa-clock-o waiting-text',
errored: 'fa-minus-circle error-text'
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| ansi.js | 15.38% | (2 / 13) | 0% | (0 / 8) | 0% | (0 / 1) | 22.22% | (2 / 9) | |
| app.js | 29.19% | (47 / 161) | 4.55% | (1 / 22) | 0% | (0 / 10) | 29.19% | (47 / 161) | |
| auth.js | 14.37% | (24 / 167) | 0% | (0 / 70) | 0% | (0 / 38) | 15.09% | (24 / 159) | |
| backchannel.js | 10.49% | (17 / 162) | 0% | (0 / 76) | 0% | (0 / 25) | 11.33% | (17 / 150) | |
| common.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| config.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| email.js | 35.44% | (28 / 79) | 50% | (2 / 4) | 27.27% | (3 / 11) | 35.44% | (28 / 79) | |
| humane.js | 8% | (2 / 25) | 0% | (0 / 26) | 0% | (0 / 2) | 8% | (2 / 25) | |
| jobs.js | 13.11% | (16 / 122) | 0% | (0 / 76) | 0% | (0 / 18) | 15.69% | (16 / 102) | |
| libconfig.js | 63.95% | (55 / 86) | 54.76% | (23 / 42) | 75% | (6 / 8) | 62.96% | (51 / 81) | |
| logging.js | 82.61% | (19 / 23) | 50% | (2 / 4) | 50% | (3 / 6) | 82.61% | (19 / 23) | |
| middleware.js | 12.61% | (15 / 119) | 0% | (0 / 56) | 5.88% | (1 / 17) | 12.61% | (15 / 119) | |
| mongoose-shim.js | 100% | (4 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (4 / 4) | |
| plugin-path.js | 93.33% | (14 / 15) | 50% | (1 / 2) | 100% | (1 / 1) | 93.33% | (14 / 15) | |
| plugin-templates.js | 25% | (11 / 44) | 0% | (0 / 14) | 0% | (0 / 11) | 26.83% | (11 / 41) | |
| projects.js | 30% | (6 / 20) | 0% | (0 / 6) | 0% | (0 / 4) | 33.33% | (6 / 18) | |
| ssh.js | 28.57% | (4 / 14) | 100% | (0 / 0) | 0% | (0 / 1) | 28.57% | (4 / 14) | |
| swig-filters.js | 50% | (1 / 2) | 100% | (0 / 0) | 0% | (0 / 1) | 50% | (1 / 2) | |
| swig-tags.js | 23.08% | (3 / 13) | 100% | (0 / 0) | 0% | (0 / 1) | 23.08% | (3 / 13) | |
| users.js | 27.27% | (6 / 22) | 0% | (0 / 10) | 0% | (0 / 7) | 30% | (6 / 20) | |
| websockets.js | 22% | (11 / 50) | 0% | (0 / 22) | 0% | (0 / 11) | 23.91% | (11 / 46) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1 1 | 'use strict';
var ansi_up = require('ansi_up');
// Copy of client/ansi/filters/ansi.js
module.exports = function (input, plaintext) {
if (!input) return '';
if (input.length > 100000) return input;
// handle the characters for "delete line" and "move to start of line"
var startswithcr = /^[^\n]*\r[^\n]/.test(input);
input = input.replace(/^[^\n\r]*\u001b\[2K/gm, '')
.replace(/\u001b\[K[^\n\r]*/g, '')
.replace(/[^\n]*\r([^\n])/g, '$1')
.replace(/^[^\n]*\u001b\[0G/gm, '');
if (startswithcr) input = `\r${input}`;
if (plaintext) return ansi_up.ansi_to_text(input);
return ansi_up.ansi_to_html(ansi_up.escape_for_html(input));
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
// The Strider (web) app.
var debug = require('debug')('strider');
var chalk = require('chalk');
var express = require('express');
var EventEmitter = require('events').EventEmitter;
require('everypaas');
var cors = require('cors');
var path = require('path');
var swig = require('swig');
// middleware
var morganDebug = require('morgan-debug');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var compression = require('compression');
var expressSession = require('express-session');
var serveFavicon = require('serve-favicon');
var errorHandler = require('errorhandler');
var methodOverride = require('method-override');
var connectFlash = require('connect-flash');
var connectMongo = require('connect-mongo');
var mongoose = require('./mongoose-shim');
var Backchannel = require('./backchannel');
var common = require('./common');
require('./logging');
var middleware = require('./middleware');
var routes = require('./routes');
var providerRouter = require('./routes/provider');
var websockets = require('./websockets');
var models = require('./models');
var auth = require('./auth');
var pluginTemplates = require('./plugin-templates');
var pjson = require('../package.json');
var routesAdmin = require('./routes/admin');
var routesJobs = require('./routes/jobs');
var api = require('./routes/api');
var collaboratorsRouter = require('./routes/collaborators');
var apiBranches = require('./routes/api/branches');
var apiJobs = require('./routes/api/jobs');
var apiRepo = require('./routes/api/repo');
var apiConfig = require('./routes/api/config');
var mongoStore = connectMongo(expressSession);
var MONTH_IN_MILLISECONDS = 2629743000;
var env = process.env.NODE_ENV || 'development';
var isDevelopment = env === 'development';
var isProduction = env === 'production';
var isTest = env === 'test';
var sessionStore;
exports.init = function (config) {
var mongodbUrl = config.db_uri;
debug('Using MongoDB URL: %s', mongodbUrl);
mongoose.connect(mongodbUrl, function (error) {
if (error) {
console.log('Could not connect to DB: %s', error);
process.exit(1);
}
});
mongoose.connection.on('error', function (error) {
debug('MongoDB connection error: %s', error);
});
sessionStore = new mongoStore({
mongooseConnection: mongoose.connection
});
swig.init({
root: config.viewpath,
// allows errors to be thrown and caught by express instead of suppressed by Swig
allowErrors: true,
cache: false,
filters: require('./swig-filters'),
tags: require('./swig-tags').tags,
extensions: {plugin: pluginTemplates}
});
var app = express();
if (isDevelopment) {
app.use(morganDebug('strider:http', 'dev'));
}
if (isTest) {
// awesome view testingness
require('./views-test')(app);
}
app.set('views', path.join(__dirname, 'views'));
app.engine('html', pluginTemplates.engine);
if (config.cors) {
app.use(cors(config.cors));
}
app.use(middleware.bodySetter);
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false, limit: config.body_parser_limit}));
// parse application/json
app.use(bodyParser.json({limit: config.body_parser_limit}));
app.use(cookieParser());
app.use(compression());
app.use(methodOverride());
app.use(serveFavicon(path.join(__dirname, '..', 'public', 'favicon.ico'), {maxAge: 2592000000}));
app.use(expressSession({
secret: config.session_secret, store: sessionStore,
cookie: {maxAge: MONTH_IN_MILLISECONDS},
resave: false,
saveUninitialized: true
}));
app.use(connectFlash());
app.use(function (req, res, next) {
res.locals.models = models;
next();
});
auth.setup(app); // app.use(passport) is included
app.use('/vendor', express.static(path.join(__dirname, '..', 'vendor'), {maxAge: MONTH_IN_MILLISECONDS}));
app.use(express.static(path.join(__dirname, '..', 'dist'), {maxAge: MONTH_IN_MILLISECONDS}));
app.use(express.static(path.join(__dirname, '..', 'public'), {maxAge: MONTH_IN_MILLISECONDS}));
if (!config.smtp) {
debug('No SMTP creds - forgot password flow will not work');
}
// Routes
app.get('/', routes.index);
app.get('/about', function (req, res) {
res.render('about');
});
app.get('/status', routes.status);
app.post('/login', function (req, res, next) {
if (!req.user) {
return next();
}
res.redirect('/');
}, auth.authenticate);
app.get('/logout', auth.logout);
app.get('/forgot', function (req, res) {
res.render('forgot.html', {
user: req.user,
messages: req.flash('error')
});
});
app.post('/forgot', auth.forgot);
app.get('/reset/:token', auth.reset);
app.post('/reset/:token', auth.resetPost);
// Compiled plugin config assets
app.get('/scripts/plugin-config-compiled.js', apiConfig.server('config', 'js'));
app.get('/styles/plugin-config-compiled.css', apiConfig.server('config', 'css'));
app.get('/scripts/account-plugins-compiled.js', apiConfig.server('account', 'js'));
app.get('/styles/account-plugins-compiled.css', apiConfig.server('account', 'css'));
app.get('/scripts/plugin-status-compiled.js', apiConfig.server('status', 'js'));
app.get('/styles/plugin-status-compiled.css', apiConfig.server('status', 'css'));
app.get('/admin/projects', auth.requireAdminOr401, routesAdmin.projects);
app.get('/admin/users', auth.requireAdminOr401, routesAdmin.users);
app.get('/admin/jobs', auth.requireAdminOr401, function (req, res) {
res.render('admin/jobs.html', {
version: pjson.version
});
});
app.get('/admin/make_admin', auth.requireAdminOr401, routesAdmin.makeAdmin);
app.post('/admin/remove_user', auth.requireAdminOr401, routesAdmin.removeUser);
app.get('/admin/invites', auth.requireAdminOr401, routesAdmin.invites);
app.get('/admin/:org/:repo/job/:job_id', auth.requireAdminOr401, routesAdmin.job);
app.get('/admin/plugins', auth.requireAdminOr401, routesAdmin.plugins.get);
app.put('/admin/plugins', auth.requireAdminOr401, routesAdmin.plugins.put);
app.use('/account', auth.requireUser, require('./routes/account'));
app.use('/projects', auth.requireUser, require('./routes/projects'));
// Requires at least read-only access to the repository in the path
app.get('/:org/:repo/', middleware.project, routesJobs.html);
app.put('/:org/:repo/', auth.requireUser, apiRepo.createProject);
app.get('/:org/:repo/job/:job_id?', middleware.project, routesJobs.multijob);
app.get('/:org/:repo/jobs/', middleware.project, routesJobs.jobs);
app.post('/:org/:repo/start', auth.requireUser, middleware.project, auth.requireProjectAdmin, apiJobs.jobsStart);
app.delete('/:org/:repo/cache', auth.requireUser, middleware.project, auth.requireProjectAdmin, apiRepo.clearCache);
app.delete('/:org/:repo/', middleware.project, auth.requireProjectAdmin, apiRepo.deleteProject);
// provider
app.use(providerRouter);
// collaborators
app.use(collaboratorsRouter);
// branches
app.use('/:org/:repo/branches/',
auth.requireUserOr401,
middleware.project,
auth.requireProjectAdmin,
apiBranches
);
// keygen
app.post('/:org/:repo/keygen/', auth.requireUser, middleware.project, auth.requireProjectAdmin, apiConfig.keygen);
/* Requires admin access to the repository in the path */
if ('development' === app.get('env')) {
app.get('/:org/:repo/config(/*)', auth.requireUser, middleware.project, auth.requireProjectAdmin, routes.reloadConfig, routes.config);
} else {
app.get('/:org/:repo/config(/*)', auth.requireUser, middleware.project, auth.requireProjectAdmin, routes.config);
}
app.put('/:org/:repo/config', auth.requireUser, middleware.project, auth.requireProjectAdmin, routes.setConfig);
app.all(
'/:org/:repo/config/branch/runner(/*)',
auth.requireUser,
middleware.project,
auth.requireProjectAdmin
);
app.get('/:org/:repo/config/branch/runner', routes.getRunnerConfig);
app.put('/:org/:repo/config/branch/runner', routes.setRunnerConfig);
app.put('/:org/:repo/config/branch/runner/id', routes.setRunnerId);
app.all(
'/:org/:repo/config/branch/:plugin',
auth.requireUser,
middleware.project,
auth.requireProjectAdmin,
middleware.projectPlugin
);
app.get('/:org/:repo/config/branch/:plugin', routes.getPluginConfig);
app.put('/:org/:repo/config/branch/:plugin', routes.setPluginConfig);
app.put('/:org/:repo/config/branch/', auth.requireUser, middleware.project, auth.requireProjectAdmin, routes.configureBranch);
// app.get('/api/job/:id', apiJobs.raw);
app.use('/api', api);
app.get('/api/jobs', auth.requireUserOr401, apiJobs.jobs);
// app.get('/api/jobs/:org/:repo', middleware.project, apiJobs.repoJobs);
app.use(function (req, res, next) {
var userCreatedTimestamp = 0;
if (req.user !== undefined) {
userCreatedTimestamp = parseInt(req.user.id.substr(0, 8), 16);
}
res.locals.user_created_timestamp = userCreatedTimestamp;
next();
});
common.app = app;
common.session_store = sessionStore;
//
// ### Strider Webapp Event Emitter
//
// Strider has a Node.Js Event Emitter which emits many events. This can be
// used by extensions to add extremely flexible custom handling for just about
// anything.
//
common.emitter = new EventEmitter();
return app;
};
exports.run = function (app) {
var config = require('./config');
if (isDevelopment) {
app.use(errorHandler({dumpExceptions: true, showStack: true}));
}
if (isProduction) {
app.use(errorHandler({dumpExceptions: true, showStack: false}));
}
// Custom 404 handler.
// Run after extensions, which might load static middlewares.
app.use(middleware.custom404);
// Initialize socket.io
var server = app.listen(config.port, config.host);
var sockets = websockets.init(server, sessionStore);
new Backchannel(common.emitter, sockets);
console.log(chalk.green('Express server listening on port %s in %s mode'), config.port, app.settings.env);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var crypto = require('crypto');
var BluebirdPromise = require('bluebird');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var utils = require('./utils');
var mailer = require('./email');
require('./config');
require('./logging');
var User = require('./models').User;
var randomBytes = BluebirdPromise.promisify(crypto.randomBytes);
function setupPasswordAuth() {
passport.use(new LocalStrategy({
usernameField: 'email'
}, function (username, password, done) {
console.log('username: %s', username);
User.authenticate(username, password, function (err, user) {
if (err || !user) {
console.log('no user');
return done(null, false, {message: 'Incorrect username.'});
}
return done(null, user);
});
}));
}
function registerRoutes(app) {
app.get('/register', function (req, res) {
return res.render('register.html', {});
});
app.post('/register', function (req, res) {
// Quick and dirty validation
var errors = [];
if (!req.body.invite_code) errors.push('No invite code specified');
if (!req.body.email) errors.push('Missing email');
if (!req.body.password) errors.push('Missing password');
if (errors.length) {
return res.render('register.html', {errors: errors});
}
User.registerWithInvite(
req.body.invite_code, req.body.email, req.body.password
, function (err, user) {
if (err) {
return res.render('register.html', {
errors: [err],
invite_code: req.body.invite_code,
email: req.body.email,
password: req.body.password
});
}
// Registered success:
req.login(user, function () {
res.redirect('/');
});
});
});
app.get('/login', function (req, res) {
if (req.user) {
return res.redirect('/');
}
var failed = Boolean(req.query.failed);
var errors = [];
if (failed) {
errors.push('Authentication failed, please supply a valid email/password.');
}
return res.render('login.html', {
errors: errors
});
});
}
function setup(app) {
app.registerAuthStrategy = function (strategy) {
passport.use(strategy);
};
app.authenticate = function () {
console.log('AUTHENTICATE', arguments);
var res = passport.authenticate.apply(passport, arguments);
console.log('!!!', res);
return function (req) {
console.log('>>>> AUTHENTICATE', req._passport, req._passport.instance._strategies.github);
res.apply(this, arguments);
};
};
setupPasswordAuth(app);
// serialize user on login
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// deserialize user on logout
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
app.use(passport.initialize());
app.use(passport.session());
app.use(basicAuth);
// Middleware to setup view parameters with user
app.use(function (req, res, next) {
if (req.user) {
req.user.gravatar = utils.gravatar(req.user.email);
}
res.locals.currentUser = req.user || null;
next();
});
registerRoutes(app);
}
function basicAuth(req, res, next) {
var auth = req.get('authorization');
if (!auth) return next();
var parts = auth.split(' ');
if (parts.length !== 2 || parts[0].toLowerCase() !== 'basic') return next();
var plain;
try {
plain = new Buffer(parts[1], 'base64').toString().split(':');
} catch (e) {
console.error('Invalid base64 in auth header');
return next();
}
if (plain.length < 2) {
console.error('Invalid auth header');
return next();
}
User.authenticate(plain[0], plain.slice(1).join(':'), function (err, user) {
if (err || !user) {
console.log('basic auth: user not found');
return res.status(401).send('Invalid username/password in basic auth');
}
req.user = user;
return next();
});
}
var _authenticate = passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login?failed=true'
});
function logout(req, res) {
req.logout();
res.redirect('/');
}
function forgot(req, res) {
var email = req.body.email.toLowerCase();
User.findOne({email: {$regex: new RegExp(email, 'i')}}, function (error, user) {
if (error) {
req.flash('error', 'An error occured while attempting to reset your password.');
return res.redirect('/forgot');
}
if (user) {
randomBytes(20).then(function (buffer) {
return buffer.toString('hex');
}).then(function (token) {
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
return new BluebirdPromise(function (resolve, reject) {
user.save(function (err, u) {
if (err) {
reject(err);
} else {
resolve(u);
}
});
});
}).then(function (user) {
mailer.sendPasswordReset(user);
req.flash('info', 'Please check your email for the password reset url. Thank you!');
res.redirect('/');
}).catch(function (error) {
console.error('Password reset error: ', error);
});
} else {
req.flash('error', 'We could not find a user with that email.');
return res.redirect('/forgot');
}
});
}
function reset(req, res) {
var token = req.params.token;
User.findOne({
resetPasswordToken: token,
resetPasswordExpires: {$gt: Date.now()}
}, function (err, user) {
if (!user) {
req.flash('error', 'Password reset token is invalid or has expired.');
return res.redirect('/forgot');
}
res.render('reset.html', {
token: token,
user: user
});
});
}
function resetPost(req, res) {
var password = req.body.password;
var confirmation = req.body.passwordConfirmation;
if (password === confirmation) {
User.findOne({
resetPasswordToken: req.params.token,
resetPasswordExpires: {$gt: Date.now()}
}, function (err, user) {
if (!user) {
req.flash('error', 'Password reset token is invalid or has expired.');
return res.redirect('back');
}
user.password = password;
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
user.save(function (err) {
if (err) {
req.flash('error', 'Couldn\'t save changes with new password.');
return res.redirect('/');
}
req.login(user, function (err) {
if (err) {
req.flash('error', 'You\'r user authentication was not successful.');
}
res.redirect('/');
});
});
});
} else {
res.redirect(req.header('Referer'));
}
}
// Require a logged in session
function requireUser(req, res, next) {
if (req.user) {
next();
} else {
req.session.return_to = req.url;
res.redirect('/login');
}
}
function requireUserOr401(req, res, next) {
if (req.user) {
next();
} else {
res.status(401).send('not authorized');
}
}
// Require admin privileges
function requireAdminOr401(req, res, next) {
if (!req.user || !req.user['account_level'] || req.user.account_level < 1) {
res.status(401).send('not authorized');
} else {
next();
}
}
// Require the logged-in user to have admin access to the repository in the
// URI path.
// E.g. http://striderapp.com/beyondfog/strider/latest_build
function requireProjectAdmin(req, res, next) {
if (!req.project) return res.status(404).send('Project not loaded');
if (!req.user) return res.status(401).send('No user');
var isAdmin = req.user.account_level && req.user.account_level > 0;
var notAuthed = (!req.accessLevel || req.accessLevel < 2) && !isAdmin;
if (notAuthed) return res.status(401).send('Not authorized for configuring this project');
next();
}
module.exports = {
setup: setup,
authenticate: _authenticate,
logout: logout,
forgot: forgot,
reset: reset,
resetPost: resetPost,
// Auth middleware
requireUser: requireUser,
requireUserOr401: requireUserOr401,
requireAdminOr401: requireAdminOr401,
requireProjectAdmin: requireProjectAdmin
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
/*
* Backchannel server allows workers to stream messages back to the Node server
* in a line-buffered way. Uses SSL.
*/
var _ = require('lodash');
var async = require('async');
var common = require('./common');
require('./config');
var debug = require('debug')('strider:backchannel');
var jobs = require('./jobs');
var models = require('./models');
var utils = require('./utils');
var Job = models.Job;
var User = models.User;
var Project = models.Project;
function striderJson(provider, project, ref, done) {
function finished(err, contents) {
if (err || !contents) return done(err);
var data = {};
try {
data = JSON.parse(contents);
} catch (e) {
debug('Strider config is invalid JSON for', project, ref);
debug(contents);
}
done(undefined, data);
}
if (!provider.hosted) {
return provider.getFile('strider.json', ref, project.provider.config, project, finished);
}
var account = project.creator.account(project.provider.id, project.provider.account);
provider.getFile('strider.json', ref, account.config, project.provider.config, project, finished);
}
/**
* Prepare the job for execution, save to database, and fire off a `job.new` event.
*
* job is expected to be populated with:
* - a trigger
* - a ref
* - the project name
* - the user id (if applicable)
* - type
* - created timestamp
*
* XXX: should this function go in a different file? idk. We'll
* definitely move it around when we make strider OO.
*/
function prepareJob(emitter, job) {
Project.findOne({ name: job.project }).populate('creator').exec(function (err, project) {
if (err || !project) return debug('job.prepare - failed to get project', job.project, err);
// ok so the project is real, we can go ahead and save this job
var provider = common.extensions.provider[project.provider.id];
if (!provider) {
return debug('job.prepare - provider not found for project', job.project, project.provider.id);
}
Job.create(job, function (err, mjob) {
if (err) return debug('job.prepare - failed to save job', job, err);
var jjob = mjob.toJSON();
jjob.project = project;
jjob.providerConfig = project.provider.config;
jjob.fromStriderJson = true;
striderJson(provider, project, job.ref, function (err, config) {
if (err) {
if (err.status === 403 || err.statusCode === 403) {
debug('job.prepare - access to strider.json is forbidden, skipping config merge');
config = {};
jjob.fromStriderJson = false;
} else if (err.status === 404 || err.statusCode === 404) {
debug('job.prepare - strider.json not found, skipping config merge');
config = {};
jjob.fromStriderJson = false;
} else {
debug('job.prepare - error opening/processing project\'s `strider.json` file: ', err);
config = {};
jjob.fromStriderJson = false;
}
} else {
debug('Using configuration from "strider.json".');
}
var branch = project.branch(job.ref.branch || 'master');
if (!branch) {
return debug('job.prepare - branch not found', job.ref.branch || 'master', project.name);
}
branch = branch.mirror_master ? project.branch('master') : branch;
jjob.providerConfig = _.extend({}, project.provider.config, config.provider || {});
config.runner = config.runner || branch.runner;
if (!common.extensions.runner[config.runner.id]) {
debug(`Error: A job was registered for a runner that doesn't exist, i.e. "${config.runner.id}". This job will never run!`);
}
if (config) {
delete config.provider;
config = utils.mergeConfigs(branch, config);
}
emitter.emit('job.new', jjob, config);
if (!mjob.runner) mjob.runner = {};
mjob.runner.id = config.runner.id;
mjob.save()
.then(() => debug('job saved'))
.catch(e => debug(e));
});
});
});
}
function BackChannel(emitter, ws) {
this.ws = ws;
this.users = {};
this.public = {};
this.waiting = {};
emitter.on('job.prepare', prepareJob.bind(null, emitter));
emitter.on('job.new', this.newJob.bind(this));
emitter.on('browser.update', this.onUpdate.bind(this));
emitter.on('job.done', this.jobDone.bind(this, emitter));
}
BackChannel.prototype = {
send: function (project, event, args) {
if (this.users[project]) {
this.ws.send(this.users[project], [event, args, 'yours']);
}
if (this.public[project]) {
this.ws.sendPublic(this.users[project], [event, args, 'public']);
}
},
sendJobs: function (project, event, args) {
if (this.users[project]) {
this.ws.sendEach(this.users[project], function (user) {
return [event, args.map(function (job) {
job = _.assign({}, job);
job.project = _.assign({}, job.project);
job.project.access_level = User.projectAccessLevel(user, job.project);
return job;
}), 'yours'];
});
}
if (this.public[project]) {
this.ws.sendPublic(this.users[project], [event, args.map(function (job) {
job = _.assign({}, job);
job.project = _.assign({}, job.project);
job.project.access_level = 0;
return job;
}), 'public']);
}
},
newJob: function (job) {
debug('new job was created');
var self = this;
var name = job.project.name;
this.waiting[name] = [];
this.public[name] = job.project.public;
async.parallel({
collaborators: function (paraCallback) {
User.collaborators(name, 0, function (err, users) {
paraCallback(err, users);
});
},
admins: function (paraCallback) {
User.admins(paraCallback);
}
}, function (err, users) {
if (err) return debug('new job: Failed to query for users');
if (!users.collaborators) return debug('new job: no users found');
self.users[name] = [];
users.collaborators.forEach(function (user) {
self.users[name].push(user._id.toString());
});
// also send to system admins
users.admins.forEach(function (user) {
self.users[name].push(user._id.toString());
});
// Admins maybe collaborators, so unique the array
self.users[name] = _.uniq(self.users[name]);
var njob = jobs.small(job);
njob.project = utils.sanitizeProject(job.project);
self.sendJobs(name, 'job.new', [njob]);
var waiting = self.waiting[name];
if (Array.isArray(waiting)) {
waiting.forEach(function (item) {
self.send.apply(self, [name].concat(item));
});
}
delete self.waiting[name];
});
},
// [project name, event name, [list of arguments]]
onUpdate: function (project, event, args) {
if (this.waiting[project]) {
return this.waiting[project].push([event, args]);
}
this.send(project, event, args);
if (event === 'job.status.started') {
Job.findById(args[0], function (err, job) {
if (err) return debug('[backchannel][job.status.started] error getting job', args[0], err);
if (!job) return debug('[backchannel][job.status.started] job not found', args[0]);
job.started = args[1];
job.save();
});
}
},
jobDone: function (emitter, data) {
var self = this;
Job.findById(data.id, function (err, job) {
if (err) return debug('Error finding job', err.message);
if (!job) return debug('job.done but job not found:', data.id);
_.extend(job, data);
try {
job.duration = data.finished.getTime() - data.started.getTime();
} catch (ignore) {
job.duration = 1;
}
job.markModified('phases');
job.markModified('plugin_data');
job.test_exitcode = job.phases.test && job.phases.test.exitCode;
job.deploy_exitcode = job.phases.deploy && job.phases.deploy.exitCode;
job.save();
job = job.toJSON();
Project.findOne({name: job.project}).lean().exec(function (err, project) {
if (err) return debug('Error finding project for job', err.message, job.project);
if (!project) return debug('Project for job.done not found', job.project);
job.project = utils.sanitizeProject(project);
job.status = jobs.status(job);
self.sendJobs(project.name, 'job.done', [job]);
emitter.emit('job.doneAndSaved', job);
});
});
}
};
module.exports = BackChannel;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 | 'use strict';
// Object for storing common state between modules.
module.exports = {
project_types: {
'node.js': {
description: 'nave, npm install, npm test',
plugins: ['node']
},
python: {
description: 'virtualenv, pip install, ./setup.py test or py.test',
plugins: ['python']
},
ruby: {
description: 'bundle install, bundle exec rake test',
plugins: ['ruby']
},
go: {
description: 'go get, go test',
plugins: ['go']
},
web: {
description: 'sauce, qunit, and jelly to test your javascript in a browser.',
plugins: ['sauce', 'qunit', 'jelly']
},
custom: {
description: 'Don\'t auto-enable any plugins.',
plugins: []
}
}
};
|
| 1 2 3 4 5 | 1 | 'use strict';
module.exports = require('./libconfig').getConfig();
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 14 14 2 1 14 1 1 1 1 1 1 1 1 | 'use strict';
var striderMailer = require('strider-mailer');
var pug = require('pug');
var fs = require('fs');
var path = require('path');
var config = require('./config');
var mailer = striderMailer(config);
var templateBasePath = path.join(__dirname, 'views/email_templates');
var templates = [
'invite',
'revoke_invite',
'notify_password_change',
'send_password_reset',
'notify_email_change',
'collaborator_invite_new_user',
'collaborator_invite_existing_user'
];
var html = loadTemplates(templates, 'html');
var text = loadTemplates(templates, 'plaintext');
/*
* loading all of the email templates at server start
*/
function loadTemplates(list, type) {
Iif (!list) {
return;
}
var result = {};
type = type || 'plaintext';
list.forEach(function (name) {
var templatePath = path.join(templateBasePath, type, `${name}.pug`);
result[name] = renderPug(templatePath);
});
return result;
}
function renderPug(filepath) {
return pug.compile(fs.readFileSync(filepath, 'utf8'), { filename: filepath });
}
exports.sendInvite = function (code, email) {
var subject = 'Strider Invitation';
var bodyHtml = html.invite({code: code, strider_server_name: config.server_name});
var bodyText = text.invite({code: code, strider_server_name: config.server_name});
mailer.send(email, subject, bodyText, bodyHtml);
};
exports.revokeInvite = function (code, email) {
var subject = 'Strider Invitation';
var bodyHtml = html.revoke_invite({code: code, strider_server_name: config.server_name});
var bodyText = text.revoke_invite({code: code, strider_server_name: config.server_name});
mailer.send(email, subject, bodyText, bodyHtml);
};
exports.notifyPasswordChange = function (user) {
var currentTime = new Date();
var subject = `[STRIDER] - Password Change Notification - ${currentTime.getHours()}:${currentTime.getMinutes()}`;
var bodyText = text.notify_password_change();
var bodyHtml = html.notify_password_change();
var to = user.email;
mailer.send(to, subject, bodyText, bodyHtml);
};
exports.sendPasswordReset = function (user) {
var currentTime = new Date();
var templateOptions = {
token: user.resetPasswordToken,
strider_server_name: config.server_name
};
var subject = `[STRIDER] - Password Reset - ${currentTime.getHours()}:${currentTime.getMinutes()}`;
var bodyText = text.send_password_reset(templateOptions);
var bodyHtml = html.send_password_reset(templateOptions);
var to = user.email;
mailer.send(to, subject, bodyText, bodyHtml);
};
exports.notifyEmailChange = function (user, oldEmail) {
var currentTime = new Date();
var subject = `[STRIDER] - Email Address Change Notification - ${currentTime.getHours()}:${currentTime.getMinutes()}`;
var to = user.email;
var bodyText = text.notify_email_change({ old_email: oldEmail, user: user });
var bodyHtml = html.notify_email_change({ old_email: oldEmail, user: user });
mailer.send(to, subject, bodyText, bodyHtml);
mailer.send(oldEmail, subject, bodyText, bodyHtml);
console.log(`send email change notification to ${oldEmail} and ${to}`);
};
exports.sendInviteCollaboratorNewUser = function (inviter, email, code, url) {
// not actually properly capitalized right now
var displayName = url.replace(/^.*com\//gi, '');
var subject = `[STRIDER] Invite to ${displayName}`;
var to = email;
var pugVariables = {
inviter: inviter.email,
display_name: displayName,
code: code,
strider_server_name: config.server_name
};
var bodyText = text.collaborator_invite_new_user(pugVariables);
var bodyHtml = html.collaborator_invite_new_user(pugVariables);
mailer.send(to, subject, bodyText, bodyHtml);
console.log(`send collaborator invite to new user ${email} for ${displayName}`);
};
exports.sendInviteCollaboratorExistingUser = function (req, email, url) {
// not actually properly capitalized right now
var displayName = url.replace(/^.*com\//gi, '');
var subject = `[STRIDER] Invite to ${displayName}`;
var to = email;
var pugVariables = {
inviter: req.user.email,
display_name: displayName,
strider_server_name: config.server_name
};
var bodyText = text.collaborator_invite_existing_user(pugVariables);
var bodyHtml = html.collaborator_invite_existing_user(pugVariables);
mailer.send(to, subject, bodyText, bodyHtml);
console.log(`send collaborator invite to existing user ${email} for ${displayName}`);
};
exports.notifyNewAdmin = function (user, email) {
var currentTime = new Date();
var subject = `[STRIDER] - New Admin - ${user} - ${currentTime.getHours()}:${currentTime.getMinutes()}`;
var body = `Hello Core,\n\nYou have a new admin colleague: ${user}\n\nHopefully this isn't unexpected.\n\nRegards,\n - StriderCD.com\n Brilliant Continuous Delivery`;
mailer.send(email, subject, body, body);
console.log(`Sending admin notification for new admin ${user}`);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1 1 | 'use strict'; /* * Javascript Humane Dates * Copyright (c) 2008 Dean Landolt (deanlandolt.com) * Re-write by Zach Leatherman (zachleat.com) * * Adopted from the John Resig's pretty.js * at http://ejohn.org/blog/javascript-pretty-date * and henrah's proposed modification * at http://ejohn.org/blog/javascript-pretty-date/#comment-297458 * * Licensed under the MIT license. */ exports.humaneDate = function humaneDate(date, compareTo) { if (!date) { return; } var lang = { ago: 'Ago', from: '', now: 'Just Now', minute: 'Minute', minutes: 'Minutes', hour: 'Hour', hours: 'Hours', day: 'Day', days: 'Days', week: 'Week', weeks: 'Weeks', month: 'Month', months: 'Months', year: 'Year', years: 'Years' }; var formats = [ [60, lang.now], [3600, lang.minute, lang.minutes, 60], // 60 minutes, 1 minute [86400, lang.hour, lang.hours, 3600], // 24 hours, 1 hour [604800, lang.day, lang.days, 86400], // 7 days, 1 day [2628000, lang.week, lang.weeks, 604800], // ~1 month, 1 week [31536000, lang.month, lang.months, 2628000], // 1 year, ~1 month [Infinity, lang.year, lang.years, 31536000] // Infinity, 1 year ]; var isString = typeof date == 'string'; date = isString ? new Date(date.replace(/-/g, '/').replace(/[TZ]/g, ' ')) : date; compareTo = compareTo || new Date; var seconds = (compareTo - date + (compareTo.getTimezoneOffset() - // if we received a GMT time from a string, doesn't include time zone bias // if we got a date object, the time zone is built in, we need to remove it. (isString ? 0 : date.getTimezoneOffset()) ) * 60000 ) / 1000; var token; if (seconds < 0) { seconds = Math.abs(seconds); token = lang.from ? ` ${lang.from}` : ''; } else { token = lang.ago ? ` ${lang.ago}` : ''; } /* * 0 seconds && < 60 seconds Now * 60 seconds 1 Minute * > 60 seconds && < 60 minutes X Minutes * 60 minutes 1 Hour * > 60 minutes && < 24 hours X Hours * 24 hours 1 Day * > 24 hours && < 7 days X Days * 7 days 1 Week * > 7 days && < ~ 1 Month X Weeks * ~ 1 Month 1 Month * > ~ 1 Month && < 1 Year X Months * 1 Year 1 Year * > 1 Year X Years * * Single units are +10%. 1 Year shows first at 1 Year + 10% */ function normalize(val, single) { var margin = 0.1; if (val >= single && val <= single * (1 + margin)) { return single; } return val; } for (var i = 0, format = formats[0]; formats[i]; format = formats[++i]) { if (seconds < format[0]) { if (i === 0) { // Now return format[1]; } var val = Math.ceil(normalize(seconds, format[3]) / (format[3])); return `${val} ${(val != 1 ? format[2] : format[1])}${(i > 0 ? token : '')}`; } } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
module.exports = {
latestJobs: latestJobs,
jobProject: jobProject,
sort: jobSort,
status: status,
small: small
};
var async = require('async');
var Job = require('./models').Job;
var User = require('./models').User;
var Project = require('./models').Project;
var utils = require('./utils');
var TEST_ONLY = 'TEST_ONLY';
/**
* user: user object
* small: if true, "phases" and "std" will not be fetched for the jobs
* - dramatically reducing the size of the object.
* TODO: add paging
* done(err, {yours: [...], public: [...]})
*
* @param {Object} user
* @param {Boolean} small
* @param {Function} done
*/
function latestJobs(user, small, done) {
if (arguments.length === 2) {
done = small;
small = false;
}
var tasks = { public: latestPublicJobs.bind(null, user, small) };
if (user) {
tasks.yours = latestUsersJobs.bind(null, user, small);
}
async.parallel(tasks, done);
}
// failed, passed, errored, running, submitted
function status(job) {
if (job.errored) return 'errored';
if (!job.started) return 'submitted';
if (!job.finished) return 'running';
if (job.test_exitcode !== 0) return 'failed';
if (job.type !== TEST_ONLY && job.deploy_exitcode !== 0) return 'failed';
return 'passed';
}
function small(job) {
var big = ['phases', 'plugin_data', 'std', 'stderr', 'stdout', 'stdmerged'];
var njob = {};
if (job.toJSON) job = job.toJSON();
for (var name in job) {
if (big.indexOf(name) !== -1) continue;
njob[name] = job[name];
}
njob.status = status(job);
return njob;
}
function jobProject(project, prev, user) {
prev.forEach(function (job) {
job.status = status(job);
});
project = utils.sanitizeProject(project);
project.prev = prev;
if (user) {
project.access_level = User.projectAccessLevel(user, project);
}
return project;
}
function latestJob(project, user, small, done) {
var query = Job.find({project: project.name.toLowerCase(), archived: null})
.sort({finished: -1})
.limit(6)
.lean();
if (small) {
query = query.select('-phases -std');
}
query.exec(function (err, jobs) {
if (!jobs || !jobs.length) {
return done(err, { nojobs: true, project: jobProject(project, [], user) });
}
var job = jobs[0];
job.project = jobProject(project, jobs.slice(1));
job.project.access_level = User.projectAccessLevel(user, project);
job.status = status(job);
done(err, job);
});
}
function projectJobs(projects, user, small, done) {
if (arguments.length === 2) {
done = small;
small = false;
}
var tasks = [];
projects.forEach(function (project) {
tasks.push(latestJob.bind(null, project, user, small));
});
async.parallel(tasks, function (err, jobs) {
if (err) return done(err);
jobs.sort(jobSort);
done(null, jobs);
});
}
function latestPublicJobs(user, small, done) {
// If we are an admin, all projects will be returned in UserJobs
if (user && user.account_level > 0) return done(null, []);
if (arguments.length === 2) {
done = small;
small = false;
}
var query = Project.find({public: true}).lean();
if (user) {
var userProjects = user.projects.map(function (p) {
return p.name.toLowerCase();
});
query = query.where('name', {$not: {$in: userProjects || []}});
}
query.exec(function (err, projects) {
if (err) return done(err);
projectJobs(projects, user, small, function (err, jobs) {
if (err) return done(err);
done(null, jobs.map(function (job) {
job.project.access_level = 0;
return job;
}));
});
});
}
function latestUsersJobs(user, small, done) {
if (arguments.length === 2) {
done = small;
small = false;
}
Project.forUser(user, function (err, projects) {
if (err) return done(err);
projectJobs(projects, user, small, done);
});
}
function jobSort(a, b) {
if (a.nojobs) {
if (b.nojobs) return 0;
return -1;
}
if (b.nojobs) return 1;
if (a.status === 'running') {
if (b.status === 'running') return 0;
return -1;
}
if (b.status === 'running') return 1;
if (a.status === 'submitted') {
if (b.status === 'submitted') return 0;
return -1;
}
if (b.status === 'submitted') return 1;
if (!a.finished || !a.finished.getTime) return -1;
if (!b.finished || !b.finished.getTime) return 1;
return b.finished.getTime() - a.finished.getTime();
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 268 1 1 1 268 1 267 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var fs = require('fs');
var path = require('path');
var everypaas = require('everypaas');
var _ = require('lodash');
var debug = require('debug')('strider:config');
var pjson = require('../package.json');
var hasGithub = pjson && pjson.dependencies['strider-github'];
var envDefaults = {
host: process.env.HOST || '0.0.0.0',
port: process.env.PORT || 3000,
server_name: 'http://localhost',
db_uri: everypaas.getMongodbUrl() || 'mongodb://localhost/strider-foss',
smtp_host: '',
smtp_port: 587,
smtp_user: '',
smtp_pass: '',
smtp_from: 'Strider <noreply@stridercd.com>',
smtp_secure: '',
enablePty: false,
extpath: 'node_modules',
session_secret: '8L8BudMkqBUqrz',
github_app_id: '',
github_secret: '',
cors: false,
body_parser_limit: false,
ldap: false
};
envDefaults.server_name = `${envDefaults.server_name}:${envDefaults.port}`;
var defaults = _.extend({
// Logging configuration
logging: {
exitOnError: true,
file_enabled: false,
console: {
// Log everything
level: 0,
colorize: true,
timestamp: true
},
console_enabled: true
},
viewpath: path.join(__dirname, 'views')
}, envDefaults);
module.exports = {
defaults: defaults,
deprecated: deprecated,
smtp: smtp,
filterEnv: filterEnv,
addPlugins: addPlugins,
camel: camel,
getConfig: getConfig
};
// main function. Get the config, using rc
function getConfig() {
process.env = filterEnv(deprecated(process.env), envDefaults);
var rc = require('rc')('strider', defaults);
Eif (!rc.smtp) rc.smtp = smtp(rc);
Eif (!rc.smtp) rc.stubSmtp = true;
rc.ldap = getConfigByName('ldap');
addPlugins(rc, process.env);
// BACK COMPAT until we get strider config into plugins...
Eif (hasGithub) {
rc.plugins.github = rc.plugins.github || {};
rc.plugins.github.hostname = rc.server_name;
}
debug(rc);
return rc;
}
function getConfigByName(filname) {
var configPath = path.join(__dirname, '..', `${filname}.json`);
Iif (fs.existsSync(configPath)) {
return require(configPath);
} else {
return false;
}
}
function camel(words) {
return words[0] + words.slice(1)
.map(function (word) {
return word[0].toUpperCase() + word.slice(1);
}).join('');
}
function addPlugins(rc, env) {
var parts;
Eif (!rc.plugins) rc.plugins = {};
for (var key in env) {
Eif (!key.match(/^plugin_/i)) continue;
parts = key.toLowerCase().split('_');
if (parts.length === 2) {
try {
rc.plugins[parts[1]] = JSON.parse(env[key]);
} catch (e) {
console.warn('Ignoring config because it\'s not valid JSON: ', key, env[key]);
}
continue;
}
if (!rc.plugins[parts[1]]) rc.plugins[parts[1]] = {};
rc.plugins[parts[1]][camel(parts.slice(2))] = env[key];
}
}
// Filter process.env.FOO to process.env.strider_foo for rc's benefit
function filterEnv(env, defaults) {
var res = {};
for (var k in env) {
if (defaults[k.toLowerCase()] !== undefined) {
res[`strider_${k.toLowerCase()}`] = env[k];
} else {
res[k] = env[k];
}
}
return res;
}
function deprecated(env) {
var nenv = _.extend({}, env);
Iif (env.APP_ID) {
console.warn('WARNING: You are using APP_ID to configure Github OAuth application id.');
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_ID instead.\n');
nenv.PLUGIN_GITHUB_APP_ID = env.APP_ID;
}
Iif (env.APP_SECRET) {
console.warn('WARNING: You are using APP_SECRET to configure Github OAuth application secret.');
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_SECRET instead.\n');
nenv.PLUGIN_GITHUB_APP_SECRET = env.APP_SECRET;
}
Iif (env.GITHUB_APP_ID) {
console.warn('WARNING: You are using GITHUB_APP_ID to configure Github OAuth application id.');
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_ID instead.\n');
nenv.PLUGIN_GITHUB_APP_ID = env.GITHUB_APP_ID;
}
Iif (env.GITHUB_SECRET) {
console.warn('WARNING: You are using GITHUB_SECRET to configure Github OAuth application secret.');
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_APP_SECRET instead.\n');
nenv.PLUGIN_GITHUB_APP_SECRET = env.GITHUB_SECRET;
}
Iif (env.GITHUB_API_ENDPOINT) {
console.warn('WARNING: You are using GITHUB_API_ENDPOINT to configure Github API base URL.');
console.warn('This name has been deprecated. Please use PLUGIN_GITHUB_API_BASE instead.\n');
nenv.PLUGIN_GITHUB_API_BASE = env.GITHUB_API_ENDPOINT;
}
return nenv;
}
function smtp(rc) {
Eif (!rc.smtp_host) {
return;
}
var options = {
host: rc.smtp_host,
port: rc.smtp_port,
from: rc.smtp_from,
secure: rc.smtp_secure
};
if (rc.smtp_user) {
options.auth = {
user: rc.smtp_user,
pass: rc.smtp_pass
};
}
return options;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1 1 1 1 1 1 1 1 1 1 176 1 1 1 1 1 1 175 1 | 'use strict';
var util = require('util');
var winston = require('winston');
var config = require('./config');
var transports = [];
var logger;
Eif (config.logging.console_enabled) {
transports.push(new winston.transports.Console(config.logging.console));
}
Iif (config.logging.file_enabled) {
transports.push(new winston.transports.File(config.logging.file));
}
logger = new winston.Logger({
transports: transports,
exitOnError: config.logging.exitOnError
});
function formatArgs(args){
return [util.format.apply(util.format, Array.prototype.slice.call(args))];
}
console.log = function (){
logger.info.apply(logger, formatArgs(arguments));
};
console.debug = function (){
logger.debug.apply(logger, formatArgs(arguments));
};
console.info = function (){
logger.info.apply(logger, formatArgs(arguments));
};
console.warn = function (){
logger.warn.apply(logger, formatArgs(arguments));
};
console.error = function (){
logger.error.apply(logger, formatArgs(arguments));
};
module.exports = logger;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 | 'use strict';
require('./logging');
var auth = require('./auth');
var utils = require('./utils');
var common = require('./common');
var Project = require('./models').Project;
var User = require('./models').User;
module.exports = {
bodySetter: bodySetter,
requireBody: requireBody,
custom404: custom404,
anonProject: anonProject,
project: project,
projectPlugin: projectPlugin,
projectProvider: projectProvider,
// Legacy aliases - don't use these:
require_auth: auth.requireUserOr401,
require_auth_browser: auth.requireUser,
require_admin: auth.requireAdminOr401,
require_project_admin: auth.requireProjectAdmin
};
// Custom middleware to save unparsed request body to req.content
function bodySetter(req, res, next) {
if (req._post_body) {
return next();
}
req.post_body = req.post_body || '';
if ('POST' !== req.method) {
return next();
}
req._post_body = true;
req.on('data', function (chunk) {
req.post_body += chunk;
});
next();
}
// Require the specified req.body parameters, or else return a 400 with a descriptive JSON body
function requireBody(paramsList) {
return function (req, res, next) {
var errors = [];
var status = 'ok';
for (var i = 0; i < paramsList.length; i++) {
var val = req.body[paramsList[i]];
if (!val) {
errors.push(`required parameter \`${paramsList[i]}\` not found.`);
status = 'error';
}
}
if (errors.length === 0) {
next();
} else {
return res.status(400).json({
errors: errors,
status: status
});
}
};
}
function custom404(req, res, next) {
if (req.method !== 'GET') {
return next();
}
res.statusCode = 404;
res.render('doesnotexist.html');
}
// Create helper function to get or set the provifer config. Expects req.project
// `req.providerConfig` function
// providerConfig() -> return the config
// providerConfig(config, next(err)). save the config
//
// For hosted providers, the following function is also available
// accountConfig() -> return the account confi
// acountConfig(config, next(err)). save the account config
function projectProvider(req, res, next) {
var project = req.project;
req.providerConfig = function (config, next) {
if (arguments.length === 0) {
return project.provider.config;
}
project.provider.config = config;
project.markModified('provider');
project.save(next);
};
// make this conditional?
if (project.provider.account) {
var account = project.creator.account(project.provider.id, project.provider.account);
req.accountConfig = function (config, next) {
if (arguments.length === 0) {
return account.config;
}
account.config = config;
project.creator.markModified('accounts');
project.creator.save(next);
};
}
next();
}
// Get plugin config. Expects req.project
// Sets `req.pluginConfig` function
// pluginConfig() -> return the config
// pluginConfig(config, next(err)). save the config
function projectPlugin(req, res, next) {
var pluginid;
// if only 3 args, then get pluginid from params ":plugin"
if (arguments.length === 4) {
pluginid = req;
req = res;
res = next;
next = arguments[3];
} else {
pluginid = req.params.plugin;
}
var branch = req.project.branch(req.query.branch);
var plugin = null;
if (!branch) {
return res.status(404).send('Specified branch not found for the project');
}
// if it's just mirroring master
if (branch.mirror_master) {
return res.status(400).send('Branch not individually configurable');
}
for (var i = 0; i < branch.plugins.length; i++) {
if (branch.plugins[i].id === pluginid) {
plugin = branch.plugins[i];
break;
}
}
if (plugin === null) {
return res.status(404).send('Plugin not enabled for the specified project');
}
req.pluginConfig = function (config, next) {
if (arguments.length === 0) {
return plugin.config;
}
plugin.config = config;
req.project.markModified('branches');
req.project.save(function (err) {
next(err, config);
});
};
req.userConfig = function (config, next) {
if (!req.user.isProjectCreator) {
if (arguments.length === 0) {
return false;
}
return next(new Error('Current user is not the creator - cannot set the creator config'));
}
if (arguments.length === 0) {
return req.project.creator.jobplugins[pluginid];
}
var schema = common.userConfigs.job && common.userConfigs.job[pluginid];
if (!schema) {
return next(new Error(`Plugin ${pluginid} doesn't define any user config`));
}
config = utils.validateAgainstSchema(config, schema);
// TODO: validation
req.project.creator.jobplugins[pluginid] = config;
req.project.creator.markModified('jobplugins');
req.project.creator.save(function (err) {
next(err, config);
});
};
next();
}
// just link project but doesn't fail if there's no auth
function anonProject(req, res, next) {
var name = `${req.params.org}/${req.params.repo}`;
name = name.toLowerCase();
Project.findOne({name: name})
.populate('creator')
.exec(function (err, project) {
if (err) {
return res.status(500).send({
error: 'Failed to find project',
info: err
});
}
if (!project) {
return res.status(404).send('Project not found');
}
if (!project.creator) {
return res.status(400).send('Project malformed; project creator user is missing.');
}
req.project = project;
req.accessLevel = User.projectAccessLevel(req.user, project);
if (req.user && project.creator) {
req.user.isProjectCreator = project.creator._id.toString() === req.user._id.toString();
}
next();
});
}
// getProject Middleware
// assumes two url parameters, :org and :repo, and req.user
// checks user access level, and sets the following properties on the
// request object.
//
// req.project = the project
// req.accessLevel = -1 for no access, 0 for public, 1 for normal, 2 for admin
//
// Errors:
// 404: not found
// 401: not public and you don't have access
// 500: something strange happened w/ the DB lookup
function project(req, res, next) {
if (req.params.org === 'auth') {
return next();
}
anonProject(req, res, function () {
if (req.accessLevel > -1) {
return next();
}
if (!req.user) {
req.session.return_to = req.url;
return res.redirect('/login');
}
res.status(401).send('Not authorized');
});
}
|
| 1 2 3 4 5 6 7 8 9 10 | 1 1 1 1 | 'use strict';
var Promise = require('bluebird');
var mongoose = require('mongoose');
mongoose.Promise = Promise;
module.exports = mongoose;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 1 1 1 1 1 2 2 2 2 2 2 2 2 2 | 'use strict';
var fs = require('fs');
var path = require('path');
var config = require('./config');
var extpaths = config.extpath.split(':');
module.exports = function (extpath) {
var extdir = [];
for (var i in extpaths) {
// Extensions are either in ../node_modules (if local)
// or __dirname/../
extdir.push(path.resolve(__dirname, '..', extpaths[i]));
try {
fs.statSync(extdir);
} catch (e) {
extdir.pop();
extdir.push(path.resolve(__dirname, '..', extpaths[i]));
}
}
Iif (extpath) {
extdir.push(path.resolve(extpath));
}
return extdir;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var fs = require('fs');
var path = require('path');
var swig = require('swig');
var async = require('async');
// TODO - better name - this is block:func plugin map
var cache = {};
// Generate func for string template names
function registerTemplate(name, template, dir) {
cache[name] = function (context, cb) {
if (/\.html$/.test(template)){
dir = dir || '.';
template = fs.readFileSync(path.join(dir, template), 'utf8');
}
cb(null, template);
};
}
function registerBlock(block, render) {
cache[block] = render;
}
// This generates a generator that will
// render the appropriate block in a form
// suitable for async.parallel.
function getPluginTemplate(name, context) {
return function (cb) {
if (cache[name]){
cache[name](context, function (err, res) {
if (err) return cb(err);
cb(null, [name, res]);
});
} else {
cb(null, null);
}
};
}
// Express 3 Template Engine
function engine(path, options, fn) {
options.filename = path;
fs.readFile(path, 'utf8', function (err, str) {
if (err) return fn(err);
engine.render(str, options, fn);
});
}
// This Render function is a bit complicated, as we're essentially
// monkeypatching swig.render to async collect the appropriate
// extension blocks, render them, and then render them into the
// template.
//
// Because we don't know which blocks are on which page, we actually
// end up rendering the template twice - first to work out which
// blocks are needed, and second, to actually insert them.
engine.render = function (str, options, fn) {
try {
// Compile
options._striderRegister = []; // register of templates needed
options._striderBlocks = {}; // output of pluginblocks
var tmpl = swig.compile(str, options);
// Which plugins were needed?
// Render 1st pass
tmpl(options);
var exts = options._striderRegister.map(function (name) {
return getPluginTemplate(name, this);
}, options);
// Call each block of plugin
async.parallel(exts, function (err, blocks){
if (err) return fn(err);
for (var i=0; i< blocks.length; i++){
if (blocks[i]){
options._striderBlocks[blocks[i][0]] = blocks[i][1];
}
}
// Render template with the _striderBlocks
fn(null, tmpl(options));
});
} catch (err) {
fn(err);
}
};
module.exports = {
registerBlock: registerBlock,
registerTemplate: registerTemplate,
engine: engine
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 1 1 1 | 'use strict';
var utils = require('./utils');
var models = require('./models');
var Project = models.Project;
var User = models.User;
module.exports = {
allProjects: allProjects
};
// Get a sanitized listing of all projects, along with the users who have access
function allProjects(done) {
User.find({}, function (err, users) {
if (err) return done(err);
Project.find()
.sort({_id: -1})
.exec(function (err, projects) {
if (err) return done(err);
done(null, projects.map(function (project) {
project = utils.sanitizeProject(project);
project.created_date = utils.timeFromId(project._id);
project.users = [];
for (var i = 0; i < users.length; i++) {
if ('undefined' !== typeof users[i].projects[project.name]) {
project.users.push({
email: users[i].email,
access: users[i].projects[project.name]
});
}
}
return project;
}));
});
});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 1 1 1 | 'use strict';
var debug = require('debug')('strider:ssh');
var NodeRSA = require('node-rsa');
var sshpk = require('sshpk');
/**
* Generates an RSA/SSH keypair.
* @param comment {string} The comment to put on the public key.
* @param callback
*/
exports.generateKeyPair = function (comment, callback) {
debug('Generating SSH key pair...');
try {
var key = new NodeRSA();
key.generateKeyPair();
var privateKeyPem = key.exportKey('pkcs1-private-pem');
var publicKeyPem = key.exportKey('pkcs1-public-pem');
var publicKey = sshpk.parseKey(publicKeyPem, 'pem');
publicKey.comment = comment;
callback(null, privateKeyPem, publicKey.toString());
} catch (error) {
callback(error);
}
};
|
| 1 2 3 4 5 6 7 8 9 | 1 | 'use strict';
module.exports = {
scriptjson: function (input) {
return JSON.stringify(input).replace(/<\//g, '<\\/');
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 1 1 1 | 'use strict'; // Plugin block is the tag used to specify that the // contents can be overridden by extensions. function pluginBlock(indent, parser) { var template = this.args[0]; var output = ''; // Register that the template is needed, for 1st pass; output += `_context._striderRegister.push('${template}');\n`; // Generate code to see if pluginTemplates has block output += `var _pg = _context._striderBlocks['${template}'];\n`; output += 'if (_pg){ '; output += '_output += _pg;'; output += '} else {\n'; output += parser.compile.call(this, `${indent} `); output += '}\n'; return output; } pluginBlock.ends = true; module.exports = { tags: { pluginblock: pluginBlock } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 1 1 1 1 1 1 | 'use strict';
var User = require('./models').User;
var email = require('./email');
var env = process.env.NODE_ENV;
module.exports = {
makeAdmin: makeAdmin
};
function makeAdmin(user, done) {
if (typeof user !== 'string' && user.email) {
user = user.email;
}
User.update({ email: user }, { account_level: 1 }, {}, function (err, num) {
if (err) return done(err);
if (!num) return done();
console.log(`Admin status granted to: ${user}`);
// if in production, notify all other admins about new admin
if (env === 'production') {
getAdmins(function (err, admins) {
admins
.filter(function removeSelf(admin) {
return admin.email !== user.email;
})
.forEach(function notifyAdmin(admin) {
email.notifyNewAdmin(user, admin.email);
});
});
}
done(null, num);
});
}
function getAdmins(done) {
User.find({ account_level: 1 }, function (err, admins) {
done(err, admins);
});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var io = require('socket.io');
var cookieParser = require('cookie-parser');
var config = require('./config');
var common = require('./common');
var UserSocket = require('./utils/user-socket');
var expressParser = cookieParser(config.session_secret);
/*
* websockets.init()
*
* Initialize the Socket.io server.
*/
// sio: socketio server. ex: io.listen(server)
function UserSockets(sio, sessionStore) {
this.sio = sio;
this.sockets = {};
this.sessionStore = sessionStore;
//sio.enable('browser client minification'); // send minified client
//sio.enable('browser client etag'); // apply etag caching logic based on version number
//sio.enable('browser client gzip');
//sio.set('log level', 1);
//sio.set('authorization', authorize.bind(this, sessionStore))
sio.use(authorize.bind(this, sessionStore));
sio.sockets.on('connection', this.connected.bind(this));
}
module.exports = UserSockets;
UserSockets.prototype = {
addSocket: function (uid, socket) {
if (!this.sockets[uid]) {
this.sockets[uid] = new UserSocket(uid);
}
this.sockets[uid].add(socket);
},
// -> true if the socket was found and removed. false if it wasn't found
removeSocket: function (uid, socket) {
var socks = this.sockets[uid];
if (!socks) return false;
return socks.remove(socket);
},
// socket callback. Adds a new socket
connected: function (socket) {
var session = socket.handshake.session;
if (session && session.passport) {
this.addSocket(session.passport.user, socket);
} else {
console.debug('Websocket connection does not have authorization - nothing to do.');
}
},
// send a message to a number of users
// send([uid, uid, ...], arguments)
send: function (users, args) {
for (var i = 0; i < users.length; i++) {
if (!this.sockets[users[i]]) continue;
this.sockets[users[i]].emit(args);
}
},
// send a message to a number of users running callback to get args
// send([uid, uid, ...], callback)
sendEach: function (users, fn) {
for (var i = 0; i < users.length; i++) {
if (!this.sockets[users[i]] || !this.sockets[users[i]].user) continue;
this.sockets[users[i]].emit(fn(this.sockets[users[i]].user));
}
},
// send a public message - to all /but/ the specified users
sendPublic: function (users, args) {
for (var id in this.sockets) {
if (users.indexOf(id) !== -1) continue;
this.sockets[id].emit(args);
}
}
};
function authorize(sessionStore, data, next) {
if (data.handshake.headers.cookie) {
var req = data.handshake;
expressParser(req, {}, function () {
var sessionID = req.signedCookies['connect.sid'];
sessionStore.get(sessionID, function (err, session) {
if (err || !session) {
next(new Error('not authorized'));
} else {
req.session = session;
next();
}
});
});
} else {
return next(new Error('not authorized'));
}
}
module.exports.init = function (server, sessionStore) {
return common.ws = new UserSockets(io.listen(server), sessionStore);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| config.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| index.js | 100% | (3 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (3 / 3) | |
| invite.js | 100% | (4 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (4 / 4) | |
| job.js | 100% | (7 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (7 / 7) | |
| project.js | 26.67% | (12 / 45) | 0% | (0 / 20) | 0% | (0 / 8) | 26.67% | (12 / 45) | |
| user.js | 17.27% | (24 / 139) | 1.16% | (1 / 86) | 0% | (0 / 28) | 17.27% | (24 / 139) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 1 1 1 1 | 'use strict';
var mongoose = require('../mongoose-shim');
var Schema = mongoose.Schema;
var Config = new Schema({
version: Number
// is there any other configuration we want?
});
module.exports = mongoose.model('Config', Config);
module.exports.SCHEMA_VERSION = 2;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 1 1 | 'use strict';
var mongoose = require('../mongoose-shim');
mongoose.models = {};
module.exports = {
InviteCode: require('./invite'),
Job: require('./job'),
User: require('./user'),
Project: require('./project'),
Config: require('./config')
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1 1 1 1 | 'use strict';
var mongoose = require('../mongoose-shim');
var Schema = mongoose.Schema;
var InviteCodeSchema = new Schema({
code: {type: String, unique: true},
created_timestamp: Date,
consumed_timestamp: Date,
emailed_to: {type: String, index: true},
consumed_by_user: {type: Schema.ObjectId, ref: 'user'},
collaborations: [
{
project: String,
access_level: Number,
invited_by: {type: Schema.ObjectId, ref: 'user'}
}
]
});
module.exports = mongoose.model('InviteCode', InviteCodeSchema);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 1 1 1 1 1 1 1 | 'use strict';
var mongoose = require('../mongoose-shim');
var Schema = mongoose.Schema;
var TriggerNotSchema = {
type: {type: String},
author: {
id: {type: Schema.ObjectId, ref: 'user'},
url: String,
name: String,
email: String,
image: String,
username: String
},
message: String,
timestamp: Date,
url: String,
source: {}
// source looks like:
// { type: "plugin", plugin: "github" } ||
// { type: "UI", page: "dashboard" } ||
// { type: "API", app: "MyApp" }
};
var PhaseNotSchema = {
duration: Number,
finished: Date,
exitCode: Number,
commands: [{
started: Date,
duration: Number,
command: String,
comment: Boolean,
plugin: String,
out: String,
err: String,
merged: String
}]
};
var JobSchema = new Schema({
type: {type: String},
user_id: {type: Schema.ObjectId, ref: 'user'},
project: {type: String, index: true}, // should always be lower case
ref: {
// not every job is on a branch, and want arbitrary stuff here.
// so we can't specify branch, but we still want an index on it
// branch: { type: String, index: true, sparse: true }
},
trigger: TriggerNotSchema,
phases: {
environment: PhaseNotSchema,
prepare: PhaseNotSchema,
test: PhaseNotSchema,
deploy: PhaseNotSchema,
cleanup: PhaseNotSchema
},
// plugins can store any extra data here
plugin_data: {},
warnings: [{
plugin: String,
title: String,
description: String,
severity: {type: String, default: 'major', enum: ['major', 'minor']}
}],
std: {
out: String,
err: String,
merged: String
},
duration: Number,
created: {type: Date, index: true},
queued: Date,
started: Date,
finished: {type: Date, index: true},
archived: {type: Date, index: true},
test_exitcode: Number,
deploy_exitcode: Number,
errored: {type: Boolean, default: false},
error: {
message: String,
stack: String
},
runner: {
id: String,
data: {}
}
});
JobSchema.index({archived: 1, project: 1, finished: -1});
module.exports = mongoose.model('Job', JobSchema);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var mongoose = require('../mongoose-shim');
var findBranch = require('../utils').findBranch;
var Schema = mongoose.Schema;
var PluginConfig = new Schema({
id: String,
config: {},
showStatus: {
type: Boolean,
default: true
},
enabled: Boolean
});
var BranchConfig = new Schema({
active: {
type: Boolean,
default: true
},
name: { type: String },
mirror_master: {
type: Boolean,
default: true
},
deploy_on_green: {
type: Boolean,
default: true
},
deploy_on_pull_request: {
type: Boolean,
default: false
},
// ssh keypair
pubkey: String,
privkey: String,
// add the ssh keys to the ENV
envKeys: Boolean,
// job & runner plugins
plugins: [PluginConfig],
runner: {
id: String,
config: {}
},
// for persistance, not configuration
plugin_data: {}
});
var ProjectSchema = new Schema({
// name is always lower case!
name: {
type: String,
unique: true,
index: true
},
display_name: { type: String }, // display name can be mixed case, for display
public: {
type: Boolean,
default: false,
index: true
},
display_url: String,
// grab the `.strider.json` in advance - could be expensive for some
// providers (like raw git). This allows runner configuration in the
// .strider.json file.
prefetch_config: {
type: Boolean,
default: true
},
// used for user-level provider & plugin config.
creator: {
type: Schema.ObjectId,
ref: 'user',
index: true
},
// looks like:
// { master: BranchConfig, otherbranch: 'master' || BranchConfig, ... }
// if a branch maps to 'master', it has all of the same configuration, but 'deploy_on_green' is false
branches: [BranchConfig],
provider: {
id: String, // plugin id of the provider
account: String, // account id
repo_id: String, // id of the repository
config: { // decided by the provider
// url: String
}
}
});
// name: the name of the new branch
// done(err)
ProjectSchema.method('addBranch', function (name, done) {
var branch = {
name: name,
mirror_master: true
};
this.branches.push(branch);
this.collection.update({ _id: this._id }, {
$push: { branches: branch }
}, function (err, changed) {
if (err) {
return done(err);
}
if (!changed) {
return done(new Error('no projects affected by adding the branch'));
}
done(null, branch);
});
});
ProjectSchema.method('cloneBranch', function (name, cloneName, done) {
var clone;
this.branches.forEach(function (branch) {
if (branch.name === name) {
clone = _.merge({}, branch);
}
});
if (!clone) {
return done(new Error('source branch can not be found'));
}
clone.name = cloneName;
this.branches.push(clone);
this.collection.update({ _id: this._id }, {
$push: { branches: clone }
}, function (err, changed) {
if (err) {
return done(err);
}
if (!changed) {
return done(new Error('no projects affected by cloning the branch'));
}
done(null, clone);
});
});
ProjectSchema.method('branch', function (name) {
return findBranch(this.branches, name);
});
ProjectSchema.static('forUser', function (user, done) {
// Default to all projects
var query = {};
// If we are not an admin i.e account level is not set or < 1, show only user projects
if (!user.account_level || user.account_level < 1) {
if (!user.projects) {
return done(null, []);
}
var names = user.projects.map(function (p) {
return p.name.toLowerCase();
});
if (!names.length) {
return done(null, []);
}
query = {
name: { $in: names }
};
}
this.find(query, done);
});
module.exports = mongoose.model('Project', ProjectSchema);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var bcrypt = require('bcryptjs');
var Activedirectory = require('activedirectory');
var config = require('../config');
var mongoose = require('../mongoose-shim');
var InviteCode = require('./invite');
var Schema = mongoose.Schema;
var User;
// active directory schema
var AdSchema = config.ldap ? new Activedirectory(config.ldap) : null;
var UserSchema = new Schema({
email: {type: String, required: true, index: true, unique: true},
salt: {type: String, required: true},
hash: {type: String, required: true},
resetPasswordToken: String,
resetPasswordExpires: Date,
// Is login by active directory, default false
isAdUser: { type: Boolean, default: false },
// 0 = normal user, 1 = strider admin
account_level: Number,
// defined by provider plugins
accounts: [{
provider: String, // name of the provider plugin
id: String, // account id, defined by the provider
title: String, // human readable account name
display_url: String, // url to view your account on the hosted site
// cache for provided repos
cache: [{
id: String, // unique ID, saved as "repo_id" in the provider section of the created project
name: String, // human readable, displayed to the user
display_name: String,
config: {},
display_url: String, // a url where the user can view the repo in a browser
group: String // a string for grouping the repos. In github, this would be the "organization"
}],
last_updated: Date,
config: {}
}],
// user-level config
jobplugins: {},
// array of objects {name: "projectname", access_level: (int), display_name: (string)}
projects: [
{
name: {type: String, index: true}, // lower-case canonical name
display_name: String, // could be mixed case
access_level: Number // 0 - view jobs, 1 - start jobs, 2 - configure/admin
}
],
jobs: [{type: Schema.ObjectId, ref: 'Job'}],
created: Date
});
UserSchema.virtual('password')
.get(function () {
return this._password;
})
.set(function (password) {
this._password = password;
var salt = this.salt = bcrypt.genSaltSync(10);
this.hash = bcrypt.hashSync(password, salt);
});
// User.collaborators(project, [accessLevel,] done(err, [user, ...]))
//
// project: String name of the project
// accessLevel: int minimum access level. Defaults to 1
UserSchema.static('collaborators', function (project, accessLevel, done) {
if (arguments.length === 2) {
done = accessLevel;
accessLevel = 1;
}
var query = {
'projects': {
'$elemMatch': {
'name': project.toLowerCase(),
'access_level': {$gte: accessLevel}
}
}
};
this.find(query, done);
});
// User.admins(done(err, [user, ...]))
UserSchema.static('admins', function (done) {
var query = {'account_level': 1};
this.find(query, done);
});
// User.account(providerconfig)
// User.account(providerid, accountid)
// --> the account config that matches
// Throws an error if the account cannot be found.
UserSchema.method('account', function (provider, account) {
if (arguments.length === 1) {
account = provider.account;
provider = provider.id;
}
for (var i = 0; i < this.accounts.length; i++) {
if (this.accounts[i].provider == provider &&
this.accounts[i].id == account) {
return this.accounts[i];
}
}
return false;
});
UserSchema.method('verifyPassword', function (password, callback) {
bcrypt.compare(password, this.get('hash'), callback);
});
UserSchema.method('jobPluginData', function (name, config, done) {
if (!this.jobplugins) {
this.jobplugins = {};
}
if (arguments.length === 1) {
return this.jobplugins[name];
}
this.jobplugins[name] = config;
this.markModified('jobplugins');
this.save(done);
});
UserSchema.static('getUserInfoFromActiveDirectory', function (email, callback) {
AdSchema.findUser(email, function (err, user) {
if (err) {
return callback(err, true);
}
if (!user) {
return callback('No User', false);
}
return callback(null, user);
});
});
// Login by active directory
UserSchema.static('loginByActiveDirectory', function (email, password, callback) {
AdSchema.authenticate(email, password, function (err, auth) {
if (err) {
return callback(err, true);
}
if (!auth) {
return callback('No User', false);
}
return this.getUserInfoFromActiveDirectory(email, callback);
}.bind(this));
});
UserSchema.static('authenticate', function (email, password, callback) {
// Has ad config
if (config.ldap) {
this.loginByActiveDirectory(email, password, function (err, adUser) {
console.log(`Active directory login msg: ${err}, User info`, adUser);
if (err && !adUser) {
this.findOne({email: email, isAdUser: false}, function (err, user) {
if (err) {
return callback(err);
}
if (!user) {
return callback('No User', false);
}
user.verifyPassword(password, function (err, passwordCorrect) {
if (err) {
return callback(err);
}
if (!passwordCorrect) {
return callback('Incorrect Password', false);
}
return callback(null, user);
});
});
} else if(err) {
return callback(err);
}
this.findOne({email: email, isAdUser: true}, function (err, user) {
if (err) {
return callback(err);
}
if (!user) {
var isAdmin = false;
if (config.ldap.adminDN && adUser.dn.indexOf(config.ldap.adminDN) !== -1) {
isAdmin = true;
}
// register and return new user
return this.register({
isAdUser: true,
isAdmin: isAdmin,
email: adUser.mail
}, callback);
}
return callback(null, user);
}.bind(this));
}.bind(this));
} else {
// Normal login
this.findOne({email: email}, function (err, user) {
if (err) {
return callback(err);
}
if (!user) {
return callback('No User', false);
}
user.verifyPassword(password, function (err, passwordCorrect) {
if (err) {
return callback(err);
}
if (!passwordCorrect) {
return callback('Incorrect Password', false);
}
return callback(null, user);
});
});
}
});
UserSchema.static('findByEmail', function (email, cb) {
this.find({email: {$regex: new RegExp(email, 'i')}}, cb);
});
UserSchema.static('register', function (u, callback) {
// Create User
var user = new User();
user.isAdUser = !!u.isAdUser;
user.account_level = u.isAdmin ? 1 : 0;
user.email = u.email.toLowerCase();
user.created = new Date();
user.set('password', u.isAdUser ? '' : u.password);
user.projects = u.projects || [];
user.save(function (error, user) {
if (error) {
return callback(`Error Creating User:${error}`);
}
callback(null, user);
});
});
UserSchema.static('registerWithInvite', function (inviteCode, email, password, cb) {
// Check Invite Code
InviteCode.findOne({
code: inviteCode,
emailed_to: email,
consumed_timestamp: null
}, function (err, invite) {
if (err || !invite) {
return cb('Invalid Invite');
}
var projects = [];
// For each collaboration in the invite, add permissions to the repo_config
if (invite.collaborations !== undefined && invite.collaborations.length > 0) {
invite.collaborations.forEach(function (item) {
projects.push({
name: item.project.toLowerCase(),
access_level: item.access_level,
display_name: item.project.toLowerCase()
});
});
}
this.register({
isAdUser: false,
email: email,
password: password,
projects: projects
}, function (err, user) {
if (err) {
return cb(err);
}
// Mark Invite Code as used.
InviteCode.update({
code: inviteCode
}, {
'$set': {
consumed_timestamp: new Date(),
consumed_by_user: user._id
}
}, {}, function (err) {
if (err) {
return cb(`Error updating invite code, user was created: ${err}`);
} else {
return cb(null, user);
}
});
});
}.bind(this));
});
UserSchema.method('projectAccessLevel', function (project) {
if (this.account_level > 0) {
return 2;
}
if (this.projects) {
for (var i = 0; i < this.projects.length; i++) {
if (this.projects[i].name === project.name) {
return this.projects[i].access_level;
}
}
}
if (project.public) {
return 0;
}
return -1;
});
UserSchema.static('projectAccessLevel', function (user, project) {
if (user) {
return user.projectAccessLevel(project);
}
if (project.public) {
return 0;
}
return -1;
});
User = module.exports = mongoose.model('user', UserSchema);
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| from0to1.js | 12.64% | (22 / 174) | 0% | (0 / 76) | 0% | (0 / 37) | 14.01% | (22 / 157) | |
| from1to2.js | 42.86% | (3 / 7) | 100% | (0 / 0) | 0% | (0 / 1) | 42.86% | (3 / 7) | |
| index.js | 14.52% | (9 / 62) | 0% | (0 / 32) | 0% | (0 / 12) | 16.36% | (9 / 55) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var models = require('../');
var Job = models.Job;
var User = models.User;
var Project = models.Project;
var async = require('async');
var utils = require('../../utils');
module.exports = function (done) {
upgradeUsers(function (err) {
if (err) return done(err);
upgradeJobs(done);
});
};
function upgradeJobs(done) {
Job.collection.find({}, function (err, cursor) {
if (err) return done(err);
cursor.toArray(function (err, jobs) {
if (err) return done(err);
console.log('converting', jobs.length, 'jobs');
var tasks = [];
jobs.forEach(function (job) {
tasks.push(function (next) {
upgradeJob(job, next);
});
});
async.series(tasks, done);
});
});
}
function upgradeJob(job, done) {
Job.findById(job._id).lean().exec(function (err, mjob) {
if (err) return done(err);
mjob.user_id = job._owner;
mjob.project = makeName(job.repo_url);
mjob.ref = {
branch: 'master'
};
if (job.github_commit_info) {
mjob.ref.id = job.github_commit_info.id;
}
mjob.std = {
out: job.stdout || '',
err: job.stderr || '',
merged: job.stdmerged || ''
};
mjob.created = job.created_timestamp;
mjob.finished = job.finished_timestamp;
mjob.queued = mjob.created;
mjob.started = mjob.created;
mjob.duration = mjob.finished ? mjob.finished.getTime() - mjob.created.getTime() : 0;
mjob.archived = job.archived_timestamp;
mjob.trigger = makeTrigger(job, job.github_commit_info);
mjob.plugin_data = {};
if (job.tasks && job.tasks.length) {
mjob.plugin_data.sauce = job.tasks;
}
killAttrs(mjob, ['created_timestamp', 'finished_timestamp', 'archived_timestamp', '_owner', 'repo_url', 'stdout', 'stderr', 'stdmerged', 'tasks']);
mjob.phases = {
environment: {
commands: []
},
prepare: {
commands: []
},
test: {
finished: mjob.finished,
duration: mjob.duration,
exitCode: mjob.test_exitcode,
commands: [{
started: mjob.started,
duration: mjob.duration,
command: 'Legacy job output',
out: mjob.std.out,
err: mjob.std.err,
merged: mjob.std.merged
}]
},
deploy: {
commands: []
},
cleanup: {
commands: []
}
};
Job.collection.update({_id: job._id}, mjob, done);
console.log('done job', job._id);
});
}
function makeTrigger(job, commit) {
if (!commit) {
return {
type: 'manual',
author: {
id: job._owner
// TODO get more info about the user? Like email, gravatar, etc
},
message: job.type === 'TEST_AND_DEPLOY' ? 'Redeploy' : 'Retest',
timestamp: job.created_timestamp,
source: {type: 'UI', page: 'Unknown'}
};
}
commit.author.image = utils.gravatar(commit.author.email);
return {
type: 'commit',
author: commit.author,
message: commit.message,
timestamp: commit.timestamp,
url: `${job.repo_url}/commit/${commit.id}`,
source: {type: 'plugin', plugin: 'github'}
};
}
function upgradeUsers(done) {
User.collection.find({}, function (err, cursor) {
if (err) return done(err);
cursor.toArray(function (err, users) {
if (err) return done(err);
console.log('converting users', users.length);
var tasks = [];
users.forEach(function (user) {
tasks.push(function (next) {
upgradeUser(user, next);
});
});
async.series(tasks, done);
});
});
}
function makeGithubRepos(user) {
if (!user.github) return [];
var github = user.github_metadata[user.github.id].repos;
var repos = [];
for (var i = 0; i < github.length; i++) {
repos.push({
id: `${github[i].id}`,
name: github[i].full_name && github[i].full_name.toLowerCase(),
display_name: github[i].full_name,
group: github[i].owner.login,
display_url: github[i].html_url,
config: {
url: `git://${github[i].clone_url.split('//')[1]}`,
owner: github[i].owner.login,
repo: github[i].name,
auth: {
type: 'https'
}
}
});
}
return repos;
}
// remove attributes from a model
function killAttrs(model, attrs) {
for (var i = 0; i < attrs.length; i++) {
delete model[attrs[i]];
}
}
function makeHerokuAccounts(user) {
if (!user.heroku || !user.heroku.length) return {accounts: [], apps: {}};
var keys = {};
var accounts = [];
var caches = {};
var apps = {};
user.heroku.forEach(function (account) {
var aid = account.account_id.split('@')[0];
if (keys[account.api_key]) {
aid = keys[account.api_key];
} else {
caches[account.api_key] = [];
}
apps[account.account_id] = {
id: account.account_id.split('@')[0].split('-')[1],
name: account.app,
account: aid,
git_url: `git@heroku.com:${account.app}.git`,
web_url: `http://${account.app}.herokuapp.com/`
};
caches[account.api_key].push(apps[account.account_id]);
if (keys[account.api_key]) {
return;
}
keys[account.api_key] = aid;
accounts.push({
id: aid,
api_key: account.api_key,
email: aid,
privkey: account.privkey,
pubkey: account.pubkey,
cache: caches[account.api_key]
});
});
return {
accounts: accounts,
apps: apps
};
}
function upgradeUser(user, done) {
User.findById(user._id).lean().exec(function (err, mongUser) {
var heroku = makeHerokuAccounts(user);
mongUser.accounts = [];
if (user.github && user.github.id) {
mongUser.accounts.push({
provider: 'github',
id: user.github.id,
display_url: `https://github.com/${user.github.login}`,
title: user.github.login,
config: convertGithub(user),
cache: makeGithubRepos(user)
});
}
mongUser.jobplugins = {
heroku: {
accounts: heroku.accounts
}
};
mongUser.projects = [];
killAttrs(mongUser, ['github', 'github_config', 'github_metadata', 'dotcloud_config', 'heroku']);
var projects = [];
if (user.github_config && user.github_config.length > 0) {
for (var i = 0; i < user.github_config.length; i++) {
var repo_config = user.github_config[i];
var name = makeName(repo_config.display_url);
mongUser.projects.push({name: name.toLowerCase(), display_name: name, access_level: 2});
projects.push(makeProject(name, repo_config, user, heroku));
}
}
Project.create(projects, function (err) {
if (err) return done(err);
User.collection.update({_id: mongUser._id}, mongUser, done);
});
console.log('projects', projects.length, user._id);
});
}
function makeName(url) {
return url.split('/').slice(-2).join('/').split('.')[0];
}
function makeProvider(name, repo, user) {
// TODO: how do I know if it's not github?
var parts = name.split('/');
var repos = user.github_metadata[user.github.id].repos;
var url = `git://${repo.display_url.split('//')[1]}.git`;
var id = null;
for (var i = 0; i < repos.length; i++) {
if (repos[i].html_url === repo.display_url) {
id = repos[i].id;
url = `git://${repos[i].git_url.split('//')[1]}`;
break;
}
}
return {
id: 'github',
repo_id: id,
account: user.github.id,
config: {
url: url,
owner: parts[0],
repo: parts[1],
auth: {
type: 'https'
// with no username specified,
}
}
};
}
// plugins that we need:
// node
//
// Optional:
// browserstack
// webhooks
// heroku
// custom
// env
// jelly
// qunit
// sauce
var checkPlugins = {
sauce: function (repo) {
if (!repo.sauce_access_key) return;
return {
access_key: repo.sauce_access_key,
username: repo.sauce_username,
browsers: repo.sauce_browsers
};
},
qunit: function (repo) {
if (!repo.qunit_file) return;
return {
file: repo.qunit_file,
path: repo.qunit_path
};
},
jelly: function (repo) {
if (!repo.jelly_payload) return;
return {
url: repo.jelly_url,
port: repo.jelly_port,
payload: repo.jelly_payload,
static: repo.jelly_static,
static_dir: repo.jelly_static_dir
};
},
env: function (repo) {
return repo.env;
},
custom: function (repo) {
return repo.custom;
},
heroku: function (repo, user, heroku) {
if (!repo.prod_deploy_target || repo.prod_deploy_target.provider !== 'heroku') return;
var account_id = repo.prod_deploy_target.account_id;
return {app: heroku.apps[account_id]};
},
webhooks: function (repo) {
if (!repo.webhooks || !repo.webhooks.length) return;
var hooks = [];
var hook;
for (var i = 0; i < repo.webhooks.length; i++) {
hook = repo.webhooks[i];
hooks.push({
id: hook._id,
url: hook.url,
secret: hook.secret,
format: '',
trigger: 'job.done'
});
}
return hooks;
},
browserstack: function (repo) {
if (!repo.browserstack_api_key) return;
return {
apiKey: repo.browserstack_api_key,
username: repo.browserstack_username,
password: repo.browserstack_password,
browsers: repo.browserstack_browsers
};
}
};
function makePlugins(repo, user, heroku) {
var plugins = [{
id: 'node',
enabled: true,
config: {
test: 'npm install',
runtime: 'whatever'
}
}];
Object.keys(checkPlugins).forEach(function (name) {
var config = checkPlugins[name](repo, user, heroku);
if (!config) return;
plugins.push({
id: name,
enabled: true,
config: config
});
});
return plugins;
}
function makeBranch(repo, user, heroku) {
return {
active: repo.active,
name: 'master',
mirror_master: false,
deploy_on_green: repo.prod_deploy_target && repo.prod_deploy_target.deploy_on_green,
deploy_on_pull_request: false,
pubkey: repo.pubkey,
privkey: repo.privkey,
plugins: makePlugins(repo, user, heroku),
plugin_data: {},
runner: {
id: 'simple-runner',
config: {
pty: false
}
}
};
}
function makeProject(name, repo, user, heroku) {
return {
name: name.toLowerCase(),
secret: repo.secret,
public: repo.public,
display_name: name,
display_url: repo.display_url,
creator: user._id,
branches: [makeBranch(repo, user, heroku)],
provider: makeProvider(name, repo, user),
};
}
function convertGithub(user) {
if (!user.github) return {};
var g = user.github;
return {
accessToken: g.accessToken,
login: g.login,
email: g.email,
gravatarId: g.gravatarId,
name: g.name
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 1 1 1 | 'use strict';
var models = require('../');
var Project = models.Project;
module.exports = function (done) {
var newBranch = {
name: '*',
mirror_master: true
};
var update = {$push: {branches: newBranch}};
var options = {multi: true};
Project.update({}, update, options, done);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 1 1 1 1 1 1 1 1 1 | 'use strict';
var async = require('async');
var models = require('../');
// done(err)
module.exports = {
ensure: ensure,
isNeeded: isNeeded,
upgrade: upgrade,
isFreshDb: isFreshDb,
needConfigObj: needConfigObj
};
var upgrades = {
0: require('./from0to1'),
1: require('./from1to2')
};
function ensure(version, done) {
isNeeded(version, function (err, needed, oldv, newv, config) {
if (err) return done(err);
if (!needed) return done();
if (process.env.STRIDER_DB_UPGRADE !== 'yes') {
console.error('Your Strider database needs to be upgraded!\n');
console.log('Please:');
console.log('1) Backup your database');
console.log('2) Re-run setting environment variable:\n');
console.log('STRIDER_DB_UPGRADE=yes');
console.log('e.g. $ env STRIDER_DB_UPGRADE=yes npm start');
process.exit(0);
} else {
console.log('STRIDER_DB_UPGRADE is set to yes');
console.log('Updating database from version %d to %d', oldv, newv);
upgrade(oldv, newv, function (err) {
if (err) return done(err);
config.save(done);
});
}
});
}
function upgrade(oldv, newv, done) {
var tasks = [];
for (var i = oldv; i < newv; i++) {
tasks.push(upgrades[i]);
}
async.series(tasks, done);
}
function isNeeded(version, done) {
models.User.find({}, function (err, users) {
models.Config.find({}, function (err, configs) {
if (err) return done(err);
if (configs && configs.length > 1) {
return done(new Error('Multiple `Config`s found in the database. Only one is allowed'));
}
if (!configs || configs.length === 0) {
var config = new models.Config({version: 0});
if (!users || !users.length) config.version = version;
return config.save(function () {
done(null, false);
});
} else {
config = configs[0];
}
if (config.version >= version) {
return done(null, false);
}
var oldversion = config.version;
config.version = version;
done(err, true, oldversion, version, config);
});
});
}
function isFreshDb(cb) {
models.User.count(function (err, res) {
if (err) throw err;
if (res === 0) {
return cb(null, true);
}
return cb('there are users.', false);
});
}
function needConfigObj(cb) {
models.Config.count(function (err, res) {
if (err) throw err;
if (res === 0) {
return cb(null, true);
}
return cb(`there are ${res}configs.`, false);
});
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| account.js | 46.67% | (7 / 15) | 0% | (0 / 2) | 0% | (0 / 3) | 46.67% | (7 / 15) | |
| index.js | 17.33% | (26 / 150) | 0% | (0 / 47) | 0% | (0 / 37) | 17.69% | (26 / 147) | |
| provider.js | 50% | (13 / 26) | 0% | (0 / 2) | 0% | (0 / 3) | 50% | (13 / 26) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 1 1 1 1 1 1 1 | 'use strict';
var express = require('express');
var pjson = require('../../package.json');
var utils = require('../utils');
var common = require('../common');
var router = express.Router();
/*
* GET /account - account settings page
*/
router.get('/', function (req, res) {
var hosted = {};
var providers = common.userConfigs.provider;
for (var id in providers) {
if (common.extensions.provider[id].hosted) {
hosted[id] = providers[id];
}
}
res.format({
html: function () {
res.render('account.html', {
user: utils.sanitizeUser(req.user.toJSON()),
providers: hosted,
userConfigs: common.userConfigs,
flash: req.flash('account'),
version: pjson.version
});
},
json: function () {
res.send({
user: utils.sanitizeUser(req.user.toJSON()),
providers: hosted,
userConfigs: common.userConfigs
});
}
});
});
module.exports = router;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var common = require('../common');
var config = require('../config');
var debug = require('debug')('strider:routes');
var jobs = require('../jobs');
var models = require('../models');
var path = require('path');
var pjson = require('../../package.json');
var User = models.User;
var utils = require('../utils');
/*
* GET home page dashboard
*/
exports.index = function (req, res) {
// Work-around for Safari/Express etags bug on cookie logout.
// Without it, Safari will cache the logged-in version despite logout!
// See https://github.com/Strider-CD/strider/issues/284
req.headers['if-none-match'] = 'no-match-for-this';
if (req.session.return_to) {
var return_to = req.session.return_to;
req.session.return_to = null;
return res.redirect(return_to);
}
var code = '';
if (req.query.code !== undefined) {
code = req.query.code;
return res.render('register.html', {
invite_code: code,
version: pjson.version
});
}
jobs.latestJobs(req.user, true, function (err, jobs) {
var availableProviders = Object.keys(common.userConfigs.provider).map(function (k) {
return common.userConfigs.provider[k];
});
res.render('index.html', {
jobs: jobs,
availableProviders: availableProviders,
flash: req.flash(),
version: pjson.version
});
});
};
exports.setConfig = function (req, res) {
var attrs = ['public'];
applyAttrs(req.project, attrs, req.body);
req.project.save(function (err) {
if (err) return res.status(500).send('failed to save project');
res.send('saved');
});
};
exports.getRunnerConfig = function (req, res) {
var branch = req.project.branch(req.query.branch);
if (!branch) {
return res.status(400).send('Invalid branch');
}
res.send(branch.runner);
};
exports.setRunnerConfig = function (req, res) {
var branch = req.project.branch(req.query.branch);
branch.runner.config = req.body;
req.project.save(function (err, project) {
if (err) {
return res.status(500).send({
error: 'Failed to save runner config'
});
}
res.send(project.branch(req.query.branch).runner.config);
});
};
exports.setRunnerId = function (req, res) {
var branch = req.project.branch(req.query.branch);
branch.runner.id = req.body.id;
branch.runner.config = req.body.config;
req.project.save(function (err, project) {
if (err) {
return res.status(500).send({
error: 'Failed to save runner config'
});
}
res.send(project.branch(req.query.branch).runner.id);
});
};
// GET /:org/:repo/config/branch/:pluginname/?branch=:branch
// Output: the config
exports.getPluginConfig = function (req, res) {
res.send(req.pluginConfig());
};
// POST /:org/:repo/config/branch/:plugin/?branch=:branch
// Set the configuration for a plugin on a branch. Output: the new config.
exports.setPluginConfig = function (req, res) {
req.pluginConfig(req.body, function (err, config) {
if (err) {
return res.status(500).send({
error: 'Failed to save plugin config'
});
}
res.send(config);
common.emitter.emit('branch.plugin_config', req.project, req.query.branch, req.params.plugin, req.body);
});
};
exports.configureBranch = function (req, res) {
var branch = req.project.branch(req.query.branch);
if (!branch) {
return res.status(400).send('Invalid branch');
}
if (req.body.plugin_order) {
return setPluginOrder(req, res, branch);
}
// TODO: move this somewhere else?
var attrs = [
'active',
'privkey',
'pubkey',
'envKeys',
'mirror_master',
'deploy_on_green',
'deploy_on_pull_request',
'runner',
'plugins'
];
applyAttrs(branch, attrs, req.body);
req.project.save(function (err) {
if (err) {
return res.status(500).send('failed to save project');
}
res.status(200).send('saved');
});
};
function setPluginOrder(req, res, branch) {
var plugins = req.body.plugin_order;
var oldPlugins = branch.plugins || [];
var map = {};
oldPlugins.forEach(function (plugin) {
map[plugin.id] = plugin;
});
plugins.forEach(function (plugin) {
if (map[plugin.id]) {
plugin.config = map[plugin.id].config;
} else {
plugin.config = {};
}
});
branch.plugins = plugins;
req.project.markModified('branches');
req.project.save(function (err) {
if (err) {
return res.status(500).send('Failed to save plugin config');
}
res.send({success: true});
common.emitter.emit('branch.plugin_order', req.project, branch.name, plugins);
});
}
exports.reloadConfig = function (req, res, next) {
common.loader.initConfig(
path.join(__dirname, 'public/javascripts/pages/config-plugins-compiled.js'),
path.join(__dirname, 'public/stylesheets/css/config-plugins-compiled.css'),
function (err, configs) {
debug('loaded config pages');
common.pluginConfigs = configs;
common.loader.initUserConfig(
path.join(__dirname, 'public/javascripts/pages/account-plugins-compiled.js'),
path.join(__dirname, 'public/stylesheets/css/account-plugins-compiled.css'),
function (err, configs) {
debug('loaded account config pages');
common.userConfigs = configs;
next();
}
);
});
};
/*
* GET /:org/:repo/config - project config page
*/
exports.config = function (req, res) {
User.collaborators(req.project.name, 0, function (err, users) {
if (err) {
throw err;
}
var data = {
version: pjson.version,
collaborators: [],
serverName: config.server_name,
project: req.project.toJSON(),
statusBlocks: common.statusBlocks,
userIsCreator: req.user.isProjectCreator
};
if (req.user.isProjectCreator) {
data.userConfigs = req.user.jobplugins;
}
delete data.project.creator;
users.forEach(function (user) {
var p = _.find(user.projects, function (p) {
return p.name === req.project.name;
});
data.collaborators.push({
email: user.email,
access: p.access_level,
gravatar: utils.gravatar(user.email),
owner: user._id.toString() === req.project.creator._id.toString(),
yourself: req.user._id.toString() === user._id.toString()
});
});
data.provider = common.pluginConfigs.provider[req.project.provider.id];
data.runners = common.pluginConfigs.runner;
data.plugins = common.pluginConfigs.job;
data.collaborators.sort(function (a, b) {
if (a.owner) return -1;
if (b.owner) return 1;
return 0;
});
var provider = common.extensions.provider[req.project.provider.id];
var creator_creds = req.project.creator.account(req.project.provider).config;
if (!provider) {
// TODO: alert the user through the UI
debug('Provider plugin not installed!', req.project.provider.id);
return respond(data);
}
if (typeof provider.getBranches === 'function' && (!provider.hosted || creator_creds)) {
provider.getBranches(creator_creds, req.project.provider.config, req.project, function (err, branches) {
data.allBranches = branches;
respond(data);
});
} else {
respond(data);
}
});
function respond(data) {
res.format({
html: function () {
res.render('project_config.html', data);
},
json: function () {
res.send(data);
}
});
}
};
/*
* /status endpoint
* Executes a simple database query to verify that system is operational.
* Assumes there is at least 1 user in the system.
* Returns 200 on success.
*
* This is for use by Pingdom and similar monitoring systems.
*/
exports.status = function (req, res) {
function error(message) {
res.statusCode = 500;
var resp = {
status: 'error',
version: `StriderCD (http://stridercd.com) ${pjson.version}`,
results: [],
errors: [{message: message}]
};
return res.jsonp(resp);
}
function ok() {
res.statusCode = 200;
var resp = {
status: 'ok',
version: `StriderCD (http://stridercd.com) ${pjson.version}`,
results: [{message: 'system operational'}],
errors: []
};
return res.jsonp(resp);
}
User.findOne(function (err, user) {
if (err) {
return error(`error retrieving user from DB: ${err}`);
}
if (!user) {
return error('no users found in DB - mis-configured?');
}
return ok();
});
};
function applyAttrs(obj, attrs, source) {
attrs.forEach(function (attr) {
if (typeof source[attr] !== 'undefined') {
obj[attr] = source[attr];
}
});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var express = require('express');
var debug = require('debug')('strider:routes:provider');
var auth = require('../auth');
var middleware = require('../middleware');
var common = require('../common');
var utils = require('../utils');
var router = new express.Router();
var rootRoute = router.route('/:org/:repo/provider');
rootRoute.all(
auth.requireUserOr401,
middleware.project,
auth.requireProjectAdmin
);
/**
* @api {get} /:org/:repo/provider Get Project Provider
* @apiUse ProjectReference
* @apiDescription Get the provider config for the specified project
* @apiName GetProjectProvider
* @apiGroup Provider
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET http://localhost:3000/strider-cd/strider/provider
*/
rootRoute.get(function (req, res) {
res.send(req.project.provider.config);
});
/**
* @api {post} /:org/:repo/provider Update Project Provider
* @apiUse ProjectReference
* @apiDescription Update a project's provider
* @apiName UpdateProjectProvider
* @apiGroup Provider
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X POST http://localhost:3000/strider-cd/strider/provider
*/
rootRoute.post(function (req, res) {
var providerId = req.project.provider.id;
debug(`Provider Id: ${providerId}`);
var providerConfig = common.extensions.provider[providerId].config;
var config = utils.validateAgainstSchema(req.body, providerConfig);
// Update project's provider config
_.extend(req.project.provider.config, config);
debug(`New provider config: ${JSON.stringify(req.project.provider.config)}`);
req.project.markModified('provider.config');
req.project.save(function (err, project) {
if (err) {
debug(`Save error: ${err.message}`);
return res.status(500).send({
error: 'Failed to save provider config'
});
}
res.send(project.provider.config);
});
});
module.exports = router;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 21.65% | (21 / 97) | 0% | (0 / 26) | 0% | (0 / 19) | 22.58% | (21 / 93) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var base32 = require('thirty-two');
var crypto = require('crypto');
var debug = require('debug')('strider:routes:admin');
var humane = require('../../humane');
var InviteCode = require('../../models').InviteCode;
var Job = require('../../models').Job;
var pjson = require('../../../package.json');
var Project = require('../../models').Project;
var projects = require('../../projects');
var Step = require('step');
var User = require('../../models').User;
var users = require('../../users');
var utils = require('../../utils');
/*
* makeInviteCode()
*
* Generate a sweet BASE32 invite code
*/
function makeInviteCode() {
var random = crypto.randomBytes(5).toString('hex');
return base32.encode(random);
}
/*
* GET /admin/invites - admin interface for invites
*/
exports.invites = function (req, res) {
InviteCode.find({}).populate('consumed_by_user').sort({'_id': -1}).exec(function (err, results) {
results.forEach(function (invite) {
invite.created = humane.humaneDate(invite.created_timestamp);
invite.consumed = humane.humaneDate(invite.consumed_timestamp);
});
res.render('admin/invites.html', {
invite_code: makeInviteCode(),
invite_codes: results,
version: pjson.version
});
});
};
/*
* GET /admin/users - admin interface for users
*/
exports.users = function (req, res) {
User.find({}).sort({'_id': -1}).exec(function (err, users) {
res.render('admin/users.html', {
flash: req.flash('admin'),
version: pjson.version,
users: users.map(function (user) {
user.created_date = humane.humaneDate(utils.timeFromId(user.id));
return user;
})
});
});
};
exports.makeAdmin = function (req, res) {
if (!req.query.user) {
return res.redirect('/admin/users');
}
users.makeAdmin(req.query.user, function (err) {
if (err) {
debug(err);
return res.send(500, 'Error making admin user');
}
res.redirect('/admin/users');
});
};
exports.removeUser = function (req, res) {
User.findOne({email: req.body.email}, function (err, user) {
if (err || !user) {
req.flash('admin', 'Failed to find user');
return res.redirect('/admin/users');
}
Project.collection.remove({
creator: user._id
}, function (err, number) {
if (err) req.flash('admin', 'Failed to remove projects');
req.flash('admin', `Removed ${number} projects owned by ${user.email}`);
user.remove(function (err) {
if (err) req.flash('admin', 'Failed to remove user');
res.redirect('/admin/users');
});
});
});
};
/*
* GET /admin/projects - admin interface for projects
*/
exports.projects = function (req, res) {
projects.allProjects(function (err, projects) {
if (err) return res.send(500, 'Error retrieving projects');
res.render('admin/projects.html', {
projects: projects,
version: pjson.version
});
});
};
/*
* GET /admin/plugins
* PUT /admin/plugins/:id */
exports.plugins = require('./plugins');
// XXX: what are we trying to do here??? - jaredly
/*
* index.admin_job - build the admin job detail page
* this is very similar although slightly different from the job function above.
* at some point they should be refactored to combine redundant func.
*/
exports.job = function (req, res) {
res.statusCode = 200;
var org = req.params.org;
var repo = req.params.repo;
var jobId = req.params.job_id;
var repoUrl = `https://github.com/${org}/${repo}`;
repoUrl = repoUrl.toLowerCase();
Step(
function runQueries() {
debug(`Querying for job id: ${jobId}`);
Job.findById(jobId).populate('_owner').exec(this.parallel());
debug(`Querying for last 20 jobs for ${repoUrl}`);
Job.find()
.sort({'finished_timestamp': -1})
.where('finished_timestamp').ne(null)
.where('repo_url', repoUrl)
.where('type').in(['TEST_ONLY', 'TEST_AND_DEPLOY'])
.limit(20)
.populate('_owner')
.exec(this.parallel());
},
function processAndRender(err, resultsDetail, results) {
if (err) throw err;
results.forEach(function (job) {
job.duration = Math.round((job.finished_timestamp - job.created_timestamp) / 1000);
job.finished_at = humane.humaneDate(job.finished_timestamp);
if (job.github_commit_info.id !== undefined) {
job.triggered_by_commit = true;
job.gravatar_url = utils.gravatar(job.github_commit_info.author.email);
if (job.github_commit_info.author.username !== undefined) {
job.committer = job.github_commit_info.author.username;
job.committer_is_username = true;
} else {
job.committer = job.github_commit_info.author.name;
job.committer_is_username = false;
}
}
job.url = `/admin/${org}/${repo}/job/${job.id}`;
});
// if resultsDetail did not return, that means this is not a valid job id
if (resultsDetail === undefined) {
res.render(404, 'invalid job id');
} else {
resultsDetail.duration = Math.round((resultsDetail.finished_timestamp - resultsDetail.created_timestamp) / 1000);
resultsDetail.finished_at = humane.humaneDate(resultsDetail.finished_timestamp);
var triggeredByCommit = false;
if (resultsDetail.github_commit_info.id !== undefined) {
triggeredByCommit = true;
resultsDetail.gravatar_url = utils.gravatar(resultsDetail.github_commit_info.author.email);
if (resultsDetail.github_commit_info.author.username !== undefined) {
resultsDetail.committer = resultsDetail.github_commit_info.author.username;
resultsDetail.committer_is_username = true;
} else {
resultsDetail.committer = resultsDetail.github_commit_info.author.name;
resultsDetail.committer_is_username = false;
}
}
resultsDetail.output = resultsDetail.stdmerged.replace(/\[(\d)?\d*m/gi, '');
var hasProdDeployTarget = false;
var adminView = true;
res.render('job.html', {
admin_view: adminView,
jobs: results,
results_detail: resultsDetail,
job_id: results[0].id.substr(0, 8),
triggered_by_commit: triggeredByCommit,
org: org,
repo: repo,
repo_url: repoUrl,
has_prod_deploy_target: hasProdDeployTarget,
version: pjson.version
});
}
}
);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| get_plugin_list.js | 27.59% | (8 / 29) | 0% | (0 / 8) | 0% | (0 / 5) | 27.59% | (8 / 29) | |
| index.js | 30.77% | (4 / 13) | 0% | (0 / 4) | 0% | (0 / 4) | 36.36% | (4 / 11) | |
| plugin_manager.js | 100% | (2 / 2) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (2 / 2) | |
| restart.js | 100% | (4 / 4) | 100% | (0 / 0) | 100% | (1 / 1) | 100% | (4 / 4) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var path = require('path');
var semver = require('semver');
require('../../../common');
var pluginPath = require('../../../plugin-path')();
var localPlugins = require('strider-cli/lib/plugin_manager/local_plugins')(pluginPath);
var client = require('strider-ecosystem-client');
module.exports = function getPluginList(cb) {
var plugins = {};
client.fetchPlugins().then(function (remotePlugins) {
Object.keys(remotePlugins).forEach(function (name) {
var remote = remotePlugins[name];
plugins[name] = {
id: name,
name: remote.name || name,
url: remote.repo,
type: remote.type,
description: remote.description,
latestVersion: remote.tag,
installedVersion: 'no',
installedPath: null,
installed: false
};
});
localPlugins.listAll(function (err, localPlugins) {
localPlugins.forEach(function (plugin) {
var known = false;
if (plugins[plugin.name]) {
known = true;
} else {
known = false;
var pkg = require(path.join(plugin.path, 'package.json'));
plugins[plugin.name] = {
id: plugin.name,
name: plugin.title || plugin.name,
description: pkg.description,
type: pkg.strider.type,
latestVersion: 'unknown'
};
}
plugins[plugin.name].installedVersion = plugin.version;
plugins[plugin.name].installedPath = plugin.path;
plugins[plugin.name].installed = true;
if (known) {
plugins[plugin.name].outdated = semver.lt(
plugins[plugin.name].installedVersion,
plugins[plugin.name].latestVersion
);
} else {
plugins[plugin.name].outdated = false;
}
});
plugins = _.sortBy(plugins, 'name');
cb(null, plugins);
});
}).error(cb).catch(cb);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 1 1 1 | 'use strict';
var restart = require('./restart');
var getPluginList = require('./get_plugin_list');
var pluginManager = require('./plugin_manager');
module.exports = {
/**
* Render a plugin management web interface
* GET /admin/plugins
*/
get: function (req, res, next) {
getPluginList(function (err, list) {
if (err) return next(err);
res.render('admin/plugins.html', {
plugins: list
});
});
},
/**
* Change a plugin (uninstall, install, upgrade)
* PUT /admin/plugins
*/
put: function (req, res) {
pluginManager[req.body.action](req.body.id, function (err) {
if (err) return res.status(500).end(err.message);
res.json({ok: 'restarting strider'});
restart();
});
}
};
|
| 1 2 3 4 5 6 7 | 1 1 | 'use strict';
var pluginPath = require('../../../plugin-path')();
module.exports = require('strider-cli/lib/plugin_manager')(pluginPath);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 1 1 1 | 'use strict';
module.exports = require('strider-cli/lib/resilient')({
/**
* Path to file to touch to restart strider
*/
restartFile: function () {
var path = require('path');
var dir = path.join(__dirname, '..', '..', '..', '..');
return path.join(dir, '.restart');
}
}).restart;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| account.js | 17.95% | (14 / 78) | 0% | (0 / 34) | 0% | (0 / 12) | 17.95% | (14 / 78) | |
| branches.js | 30.3% | (10 / 33) | 0% | (0 / 10) | 0% | (0 / 7) | 33.33% | (10 / 30) | |
| config.js | 18.18% | (8 / 44) | 0% | (0 / 16) | 0% | (0 / 9) | 21.05% | (8 / 38) | |
| index.js | 100% | (6 / 6) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (6 / 6) | |
| jobs.js | 25% | (8 / 32) | 0% | (0 / 14) | 0% | (0 / 4) | 25% | (8 / 32) | |
| repo.js | 13.93% | (17 / 122) | 0% | (0 / 70) | 0% | (0 / 20) | 14.66% | (17 / 116) | |
| session.js | 42.86% | (6 / 14) | 0% | (0 / 4) | 0% | (0 / 3) | 42.86% | (6 / 14) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var auth = require('../../auth');
var debug = require('debug')('strider:routes:api:account');
var email = require('../../email');
var express = require('express');
var models = require('../../models');
var Project = models.Project;
var router = express.Router();
var User = models.User;
var validator = require('validator');
router.use(auth.requireUserOr401);
router.route('/:provider/:id')
/**
* @api {put} /account/:provider/:id Update Provider Account
* @apiDescription Updates a provider account for the _active_ user (the API user).
* @apiName UpdateAccount
* @apiGroup Account
* @apiVersion 1.0.0
*
* @apiParam {String} provider Type of provider, e.g. github
* @apiParam {Number} id Unique provider identification
*/
.put(function (req, res) {
var accounts = req.user.accounts;
var provider = req.params.provider;
var id = req.params.id;
for (var i = 0; i < accounts.length; i++) {
if (accounts[i].provider === provider &&
accounts[i].id === id) {
// TODO validate these accounts
accounts[i] = req.body;
return User.update({_id: req.user._id}, {$set: {accounts: accounts}}, function (err) {
if (err) {
debug(err);
return res.status(500).send('Failed to save one user');
}
res.sendStatus(200);
});
}
}
accounts.push(req.body);
User.update({_id: req.user._id}, {$set: {accounts: accounts}}, function (err) {
if (err) {
debug(err);
return res.status(500).send('Failed to save one user');
}
res.sendStatus(200);
});
})
/**
* @api {delete} /account/:provider/:id Delete Provider Account
* @apiDescription Deletes a provider account for the _active_ user (the API user).
* @apiName DeleteAccount
* @apiGroup Account
* @apiVersion 1.0.0
*
* @apiParam {String} provider Type of provider, e.g. github
* @apiParam {Number} id Unique provider identification
*/
.delete(function (req, res) {
var accounts = req.user.accounts;
var provider = req.params.provider;
var id = req.params.id;
var accountRemoved = false;
Project.find({'provider.id': provider})
.lean()
.exec(function (err, projects) {
if (err) {
return res.status(400).send('Failed do to bad data');
}
if (projects.length) {
var projectNames = projects.map(function (project) {
return project.name;
});
return res.status(403)
.send(`Cannot delete provider since projects are using this provider: ${projectNames.join(', ')}`);
} else {
accounts.forEach(function (account, index) {
if (account.provider === provider && account.id === id) {
accounts.splice(index, 1);
accountRemoved = true;
}
});
if (accountRemoved) {
return req.user.save(function (err) {
if (err) {
return res.status(500).send('Failed to save user');
}
res.sendStatus(204);
});
}
res.status(404).send('Account not found');
}
});
});
router.route('/password')
/**
* @api {post} /account/password Change Password
* @apiDescription Changes the password for the _active_ user (the API user).
* @apiName ChangePassword
* @apiGroup Account
* @apiVersion 1.0.0
*
* @apiParam (RequestBody) {String{6..}} password The new password, which must be at least 6 characters long.
*/
.post(function (req, res) {
if (req.user !== undefined) {
debug(`password change by ${req.user.email}`);
}
if (req.user.isAdUser) {
return res.status(400).json({
status: 'error',
errors: [{message: 'The ldap user can not change password.'}]
});
}
var password = req.body.password;
if (password.length < 6) {
return res.status(400).json({
status: 'error',
errors: [{message: 'password must be at least 6 characters long'}]
});
}
req.user.password = password;
req.user.save(function (err) {
if (err) {
throw err;
}
email.notifyPasswordChange(req.user);
res.json({
status: 'ok',
errors: []
});
});
});
router.route('/email')
/**
* @api {post} /account/email Change Email
* @apiDescription Changes the email address for the _active_ user (the API user).
* @apiName ChangeEmail
* @apiGroup Account
* @apiVersion 1.0.0
*
* @apiParam (RequestBody) {String} email The new email address. This must be a VALID email address.
*/
.post(function (req, res) {
var newEmail = req.body.email;
if (!validator.isEmail(newEmail)) {
return res.status(400).json({
status: 'error',
errors: [{message: 'email is invalid'}]
});
}
debug(`email change from ${req.user.email} to ${newEmail}`);
if (req.user.isAdUser) {
return res.status(400).json({
status: 'error',
errors: [{message: 'The ldap user can not change email.'}]
});
}
var oldEmail = req.user.email;
req.user.email = newEmail;
req.user.save(function (err) {
if (err) {
return res.status(400).json({
status: 'error',
errors: [{message: 'email already in use'}]
});
}
email.notifyEmailChange(req.user, oldEmail);
res.json({status: 'ok', errors: []});
});
});
module.exports = router;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var express = require('express');
var Project = require('../../models').Project;
var middleware = require('../../middleware');
var router = express.Router();
var requireBody = middleware.requireBody;
var root = router.route('/');
/**
* @api {post} /:org/:repo/branches Add Branch
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Add a new branch for a project.
* @apiName AddBranch
* @apiGroup Branch
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X POST -d name=newbranch http://localhost/api/strider-cd/strider/branches
*
* @apiParam (RequestBody) {String} name The name of the new branch
* @apiParam (RequestBody) {String} cloneName The name of the cloned branch
*/
root.post(requireBody(['name']), function (req, res) {
if (req.body.cloneName) {
req.project.cloneBranch(req.body.name, req.body.cloneName, function (err, branch) {
res.send({created: true, message: 'Branch cloned', branch: branch});
});
} else {
req.project.addBranch(req.body.name, function (err, branch) {
if (err) return res.status(500).send(err.message);
res.send({created: true, message: 'Branch added', branch: branch});
});
}
});
/**
* @api {put} /:org/:repo/branches Reorder Branches
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Updates the branch order for a project.
* @apiName ReorderBranches
* @apiGroup Branch
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X PUT -d branches=master,testing http://localhost/api/strider-cd/strider/branches
*
* @apiParam (RequestBody) {String} branches The new branch order, comma delimited
*/
root.put(requireBody(['branches']), function (req, res) {
var branches = req.body.branches;
var query = {_id: req.project._id};
var update = {'$set': {branches: branches}};
Project.update(query, update, function (err) {
if (err) return res.status(500).send(err.message);
res.send({status: 'updated', message: 'Branch order updated'});
});
});
/**
* @api {delete} /:org/:repo/branches Delete Branch
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Deletes a branch from a project
* @apiName DeleteBranch
* @apiGroup Branch
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X DELETE -d name=mybranch http://localhost/api/strider-cd/strider/branches
*
* @apiParam (RequestBody) {String} name The name of the branch to delete
*/
root.delete(requireBody(['name']), function (req, res) {
var name = req.body.name;
var query = {_id: req.project._id};
var update = {'$pull': {branches: {name: name}}};
if (name.toLowerCase() === 'master') {
return res.status(400).send('Cannot remove the master branch');
}
Project.update(query, update, function (err) {
if (err) return res.status(500).send(err.message);
res.send({status: 'removed'});
});
});
module.exports = router;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 1 1 1 1 1 1 1 1 | 'use strict';
var common = require('../../common');
var debug = require('debug')('strider:routes:api:config');
var ssh = require('./../../ssh');
var cache = {};
module.exports = {
keygen: keygen,
cacheConfig: cacheConfig,
server: server
};
function keygen(req, res) {
var branch = req.project.branch(req.query.branch);
if (!branch) return res.status(404).send('Branch not found');
ssh.generateKeyPair(`${req.project.name} - stridercd`, function (err, priv, pub) {
if (err) return res.status(500).send('Failed to generate keypair');
branch.privkey = priv;
branch.pubkey = pub;
req.project.save(function (err) {
if (err) return res.status(500).send('Failed to save project');
res.send({
privkey: priv,
pubkey: pub
});
});
});
}
function cacheConfig(loader, next) {
loader.initConfig(function (err, jstext, csstext, configs) {
if (err) return next(err);
cache['config'] = {
js: jstext,
css: csstext
};
debug('loaded config pages');
common.pluginConfigs = configs;
loader.initUserConfig(function (err, jstext, csstext, configs) {
if (err) return next(err);
cache['account'] = {
js: jstext,
css: csstext
};
debug('loaded account config pages');
common.userConfigs = configs;
loader.initStatusBlocks(function (err, jstext, csstext, blocks) {
if (err) return next(err);
cache['status'] = {
js: jstext,
css: csstext
};
debug('loaded plugin status blocks');
common.statusBlocks = blocks;
next();
});
});
});
}
function server(name, which) {
return function (req, res) {
res.set('Content-type', `text/${(which === 'css' ? 'css' : 'javascript')}`);
if (!cache['config']) {
return res.status(500).send('looks like config was not compiled correctly');
}
res.send(cache[name][which]);
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 1 1 1 1 1 1 | 'use strict';
var express = require('express');
var router = new express.Router();
router.use('/account', require('./account'));
router.use('/admin', require('./admin'));
router.use('/session', require('./session'));
module.exports = router;
// Globals & Commons for ApiDoc ..
/**
* @apiDefine RequestUrl Request URL Parameters
* Indicates that this parameter should be specified in the request URL.
*/
/**
* @apiDefine RequestBody Request Body Parameters
* Indicates that this parameter should be specified in the request body.
*/
/**
* @apiDefine ProjectAdmin
* You must have admin privileges on the corresponding RepoConfig to be able to use this endpoint.
*/
/**
* @apiDefine GlobalAdmin
* You must have admin privileges in Strider (globally) in order to use this endpoint.
*/
/**
* @apiDefine ProjectReference
*
* @apiParam (RequestUrl) {String} org The organization name for the project. This is
* usually a GitHub user or organization name (e.g. "strider" in "strider-cd/strider")
* but may vary from one project provider to another. (as another example,
* in GitLab this refers to the repository's "group").
* @apiParam (RequestUrl) {String} repo The project's repository name.
*/
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 1 1 1 1 1 1 1 1 | 'use strict';
var common = require('../../common');
var debug = require('debug')('strider:routes:api:jobs');
var jobs = require('../../jobs');
var utils = require('../../utils');
var TEST_ONLY = 'TEST_ONLY';
var TEST_AND_DEPLOY = 'TEST_AND_DEPLOY';
/**
* @api {post} /:org/:repo/start Start Job
* @apiUse ProjectReference
* @apiDescription Executes a strider test and, optionally, deployment.
* @apiName StartJob
* @apiGroup Job
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X POST http://localhost/api/strider-cd/strider/start
*
* @apiParam (RequestBody) {String} type="TEST_ONLY" Denotes the type of job to run.
* This can be "TEST_ONLY", which indicates that only the test stages of the job should
* be executed or "TEST_AND_DEPLOY", which indicates that all stages should be executed.
* @apiParam (RequestBody) {String} branch="master" Indicates which branch configuration
* should be executed.
* @apiParam (RequestBody) {String} message="Manually Retesting/Redeploying" An
* optional message to include as the title of the execution.
*/
exports.jobsStart = function (req, res) {
var type = req.body.type || TEST_ONLY;
var branch = req.body.branch || 'master';
var message = req.body.message;
var now = new Date();
var trigger;
var job;
var Project = common.context.models.Project;
Project.findOne({name: req.project.name}, function (err, project) {
if (err || !project) {
return res.json(404);
}
trigger = {
type: 'manual',
author: {
id: req.user._id,
email: req.user.email,
image: utils.gravatar(req.user.email)
},
timestamp: now,
source: {type: 'UI', page: req.body.page || 'unknown'}
};
if (message) {
trigger.message = message;
} else {
if (type === TEST_AND_DEPLOY)
trigger.message = 'Manually Redeploying';
else
trigger.message = 'Manually Retesting';
}
job = {
type: type,
user_id: req.user._id,
project: req.project.name,
ref: {branch: branch},
trigger: trigger,
created: now
};
common.emitter.emit('job.prepare', job);
res.json(job);
});
};
/**
* @api {get} /api/jobs Get Latest Jobs
* @apiDescription Return JSON object containing the most recent build status for each configured repo
* This function is used to build the main dashboard status page.
* The result is separated into `{public: [], yours: []}`.
*
* Note: the private ones are just ones that the current user is a collaborator
* on and are not necessarily private
* @apiName GetJobs
* @apiGroup Job
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET http://localhost/api/jobs
*/
exports.jobs = function (req, res) {
debug('api.jobs');
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
jobs.latestJobs(req.user, function (err, jobs) {
res.send(JSON.stringify(jobs, null, '\t'));
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
/*
* Repo-specific actions - such as deactivation, deletion etc.
* routes/api/repo.js
*/
var async = require('async');
var common = require('../../common');
var debug = require('debug')('strider:routes:api:repo');
var Job = require('../../models').Job;
var Project = require('../../models').Project;
var ssh = require('./../../ssh');
var User = require('../../models').User;
var utils = require('../../utils');
function makePlugins(plugins) {
var configs = [];
var plugin;
for (var i = 0; i < plugins.length; i++) {
plugin = common.extensions.job[plugins[i]];
if (!plugin) return false;
var config = utils.defaultSchema(plugin);
configs.push({
id: plugins[i],
enabled: true,
config: config
});
}
return configs;
}
/**
* @api {delete} /:org/:repo/cache Clear Cache
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Clears/invalidates the cache for a project.
* @apiName ClearCache
* @apiGroup Repo
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X DELETE http://localhost/api/strider-cd/strider/cache
*/
exports.clearCache = function (req, res) {
clearProjectCache(req.project, function (err, result) {
if (err) {
return res.status(500).send('failed to clear cache');
}
if (result) {
res.send(result);
} else {
res.sendStatus(204);
}
});
};
function clearProjectCache(project, cb) {
var runners = [];
var tasks = [];
project.branches.forEach(function (branch) {
var nonMasterMirrored = branch.name !== 'master' && branch.mirror_master;
if (nonMasterMirrored || runners.indexOf(branch.runner.id) !== -1) {
return;
}
runners.push(branch.runner.id);
});
runners.forEach(function (rid) {
var runner = common.extensions.runner[rid];
debug(rid, common.extensions.runner, project);
if (!runner || !runner.clearCache) return;
tasks.push(runner.clearCache.bind(runner, project));
});
if (!tasks.length) {
return cb(undefined, 'No runners supported cache clearing');
}
async.parallel(tasks, cb);
}
/**
* @api {put} /:org Create Repo
* @apiDescription Create a new project for a repo.
* @apiName CreateRepo
* @apiGroup Repo
* @apiVersion 1.0.0
*
* @apiParam (RequestBody) {String} name The name of the new branch
* @apiParam (RequestBody) {String} display_name Human-readable project name
* @apiParam (RequestBody) {String} display_url The URL for the repo (e.g. Github homepage)
* @apiParam (RequestBody) {Boolean} public=false Whether this project is public or not.
* @apiParam (RequestBody) {Boolean} prefetch_config=true Whether the strider.json should be fetched in advance.
* @apiParam (RequestBody) {String} account The ID of provider account
* @apiParam (RequestBody) {String} repo_id The ID of the repo
* @apiParam (RequestBody) {Object} provider A json object with 'id' and 'config' properties.
*/
exports.createProject = function (req, res, next) {
if (req.params.org === 'auth') {
return next();
}
var name = `${req.params.org}/${req.params.repo}`;
debug(`Setting up new project "${name}"...`);
var display_name = req.body.display_name;
var display_url = req.body.display_url;
var isPublic = req.body.public === 'true' || req.body.public === '1';
var prefetch_config = true;
var project_type = req.body.project_type || 'node.js';
if (req.body.prefetch_config === 'false' || req.body.prefetch_config === '0') {
prefetch_config = false;
}
var provider = req.body.provider;
function error(code, str) {
return res.status(code).json({
results: [],
status: 'error',
errors: [{code: code, reason: str}]
});
}
if (!display_name) {
return error(400, 'display_name is required');
}
if (!provider || !provider.id) {
return error(400, 'provider.id is required');
}
if (common.extensions.provider[provider.id].hosted) {
if (!provider.account) {
return error(400, 'provider.account is required');
}
if (!provider.repo_id) {
return error(400, 'provider.repo_id is required');
}
}
if (!provider.config) {
provider.config = utils.defaultSchema(provider.config);
}
if (!common.project_types[project_type]) {
return error(400, 'Invalid project type specified');
}
var plugins = makePlugins(common.project_types[project_type].plugins);
if (!plugins) {
return error(400, 'Project type specified is not available; one or more required plugins is not installed');
}
function projectResult(err, project) {
if (project) {
debug(`User ${req.user.email} tried to create project for repo ${name}, but it already exists`);
return error(409, 'project already exists');
}
return ssh.generateKeyPair(`${name}-${req.user.email}`, createProjectWithKey);
}
function createProjectWithKey(err, privkey, pubkey) {
if (err) return error(500, 'Failed to generate ssh keypair');
var project = {
name: name,
display_name: display_name,
display_url: display_url,
public: isPublic,
prefetch_config: prefetch_config,
creator: req.user._id,
provider: provider,
branches: [
{
name: 'master',
active: true,
mirror_master: false,
deploy_on_green: true,
deploy_on_pull_request: false,
pubkey: pubkey,
privkey: privkey,
plugins: plugins,
runner: {
id: 'simple-runner',
config: {pty: false}
}
},
{
name: '*',
mirror_master: true
}
]
};
var plugin = common.extensions.provider[provider.id];
if (!plugin.hosted || !plugin.setupRepo) {
return Project.create(project, projectCreated);
}
debug(`Setting up repository "${project.name}" with provider "${provider.id}"...`);
plugin.setupRepo(req.user.account(provider).config, provider.config, project, function (err, config) {
if (err) {
debug(`Setting up repository "${project.name}" failed!`, err.status, err.message);
return error(500, `Failed to setup repo: ${err.message}`);
}
project.provider.config = config;
Project.create(project, projectCreated);
});
}
function projectCreated(err, p) {
if (err) {
debug(`Error creating repo ${name} for user ${req.user.email}: ${err}`);
debug(err.stack);
return error(500, 'internal server error');
}
// Project object created, add to User object
User.update({_id: req.user._id}, {
$push: {
projects: {
name: name,
display_name: p.display_name,
access_level: 2
}
}
}, function (err, num) {
if (err || !num) debug('Failed to give the creator repo access...');
return res.json({
project: {
_id: p._id,
name: p.name,
display_name: p.display_name
},
results: [{code: 200, message: 'project created'}],
status: 'ok',
errors: []
});
});
}
name = name.toLowerCase().replace(/ /g, '-');
Project.findOne({name: name}, projectResult);
};
/**
* @api {delete} /:org/:repo Delete Repo
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Deletes a repository/project. Also archives all jobs (marks as archived in DB which makes them hidden).
* @apiName DeleteRepo
* @apiGroup Repo
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X DELETE http://localhost/api/strider-cd/strider
*/
exports.deleteProject = function (req, res) {
async.parallel([
function (next) {
var provider = req.project.provider;
var plugin = common.extensions.provider[provider.id];
if (!plugin.hosted || !plugin.teardownRepo) return next();
plugin.teardownRepo(req.project.creator.account(provider).config, provider.config, req.project, function (err) {
if (err) debug('Error while tearing down repo', req.project.name, provider.id, err);
next();
});
},
req.project.remove.bind(req.project),
function (next) {
clearProjectCache(req.project, function (error) {
next(error);
});
},
function (next) {
var now = new Date();
Job.update({project: req.project.name},
{$set: {archived: now}},
{multi: true}, next);
}
], function (err) {
if (err) {
debug('repo.delete_index() - Error deleting repo config for url %s by user %s: %s', req.project.name, req.user.email, err);
return res.status(500).send(`Failed to delete project: ${err.message}`);
}
var r = {
errors: [],
status: 'ok',
results: []
};
res.send(JSON.stringify(r, null, '\t'));
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 1 1 1 1 1 1 | 'use strict';
var express = require('express');
var models = require('../../models');
var router = new express.Router();
var User = models.User;
router.route('/')
/**
* @api {get} /api/session Get Session
* @apiDescription Gets the current session information
* @apiName GetSession
* @apiGroup Session
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET http://localhost/api/session
*/
.get(function getSession(req, res) {
res.send({ user: req.user });
})
/**
* @api {post} /api/session Create New Session
* @apiDescription Creates a new user session after validating an email address and password pair.
* @apiName CreateSession
* @apiGroup Session
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X POST -d email=me@me.com -d password=mypass http://localhost/api/session
*
* @apiParam (RequestBody) {String} email The email address to login as (which is used as the username).
* @apiParam (RequestBody) {String} password The user's password.
*/
.post(function createSession(req, res) {
User.authenticate(req.body.email, req.body.password, function (err, user) {
if (!user) {
res.status(404).send({message: 'No such username / password'});
} else {
if (!req.session.passport) {
req.session.passport = {};
}
req.session.passport.user = user.id;
res.send(user);
}
});
});
module.exports = router;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 36.84% | (14 / 38) | 0% | (0 / 6) | 0% | (0 / 8) | 36.84% | (14 / 38) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var auth = require('../../../auth');
var debug = require('debug')('strider:routes:api:admin');
var email = require('../../../email');
var express = require('express');
var requireBody = require('../../../utils/require-body');
var router = new express.Router();
var InviteCode = require('../../../models').InviteCode;
var User = require('../../../models').User;
router.use(auth.requireAdminOr401);
/**
* @api {get} /admin/users Get All Users
* @apiPermission GlobalAdmin
* @apiDescription Retrieves a list of all Strider users.
* @apiName GetUsers
* @apiGroup Admin
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET http://localhost/admin/users
*/
router.route('/users')
.get(function (req, res) {
var users = [];
User.find({}).sort({email: 1}).exec(function (err, results) {
results.forEach(function (user) {
users.push({
id: user.id,
email: user.email
});
});
res.json(users);
});
});
/**
* @apiIgnore
* @api {get} /admin/jobs Get Job Status
* @apiPermission GlobalAdmin
* @apiDescription Returns a JSON object of the last 100 jobs executed.
* @apiName GetJobs
* @apiGroup Admin
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET http://localhost/admin/jobs
*/
router.route('/jobs')
.get(function (req, res) {
res.status(500).send('Not yet implemented');
});
/**
* @api {post} /admin/invite/new Send Invite
* @apiPermission GlobalAdmin
* @apiDescription Create & email a new Strider invite.
* @apiName SendInvite
* @apiGroup Admin
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X POST -d invite_code=xoxox -d email=new_guy@strider-cd.com http://localhost/invite/new
*
* @apiParam (RequestBody) {String} invite_code The invite code/token to use in the invitation
* @apiParam (RequestBody) {String} email The email address of the new user being invited
*/
router.route('/invite/new')
.post(function (req, res) {
var inviteCode = requireBody('invite_code', req, res);
var emailAddr = requireBody('email', req, res);
if (inviteCode === undefined || emailAddr === undefined) {
return;
}
var invite = new InviteCode();
invite.code = inviteCode;
invite.created_timestamp = new Date();
invite.emailed_to = emailAddr;
invite.save(function (err, invite) {
email.sendInvite(invite.code, emailAddr);
res.redirect('/admin/invites');
});
});
/**
* @api {post} /admin/invite/revoke Revoke Invite
* @apiPermission GlobalAdmin
* @apiDescription Revokes a previously sent Strider invitation.
* @apiName RevokeInvite
* @apiGroup Admin
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X POST -d invite_code=xoxox http://localhost/invite/revoke
*
* @apiParam (RequestBody) {String} invite_code The invite code/token of the invite
* being revoked.
*/
router.route('/invite/revoke')
.post(function (req, res) {
var inviteCode = requireBody('invite_code', req, res);
InviteCode.remove({code: inviteCode, consumed_timestamp: {$exists: false}}, function (err) {
if (err) {
debug(err);
return res.status(500).send('Error revoking invite');
}
email.revokeInvite(inviteCode);
res.redirect('/admin/invites');
});
});
module.exports = router;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var crypto = require('crypto');
var base32 = require('thirty-two');
var mail = require('../../email');
var models = require('../../models');
var User = models.User;
var InviteCode = models.InviteCode;
module.exports = {
add: add,
del: del
};
function updateInvite(invite, collaboration, done) {
var found = _.find(invite.collaborations, function (c) {
return c.project === collaboration.project;
});
// There is already an outstanding invite to this user, just push these additional perms onto the collaborations
// list and send another email. We only push if they have not already been added to the invite.
if (!found) {
invite.update({$push: {collaborations: collaboration}}, function (err) {
if (err) return done(err);
// Invite updated, should probably send another email to recipient.
return done(null, false, false);
});
} else {
return done(null, false, true);
}
}
function sendInvite(inviter, email, collaboration, done) {
var random = crypto.randomBytes(5).toString('hex');
var invite_code = base32.encode(random);
var invite = new InviteCode({
code: invite_code,
emailed_to: email,
created_timestamp: new Date(),
collaborations: [collaboration]
});
invite.save(function (err) {
if (err) return done(err);
// Invite created, send email to recipient.
mail.sendInviteCollaboratorNewUser(inviter, email, invite_code, collaboration.project);
return done(null, false, false);
});
}
// done(err, userExisted, inviteExisted)
function add(project, email, accessLevel, inviter, done) {
User.findOne({email: email}, function (err, user) {
if (err) {
return done(err);
}
if (user) {
var p = _.find(user.projects, function (p) {
return p.name === project.toLowerCase();
});
if (p) {
return done('user already a collaborator', true);
}
User.update({email: email}, {
$push: {
'projects': {
name: project.toLowerCase(),
display_name: project,
access_level: accessLevel
}
}
}, function (err) {
if (err) return done(err, true);
done(null, true);
});
} else {
var collaboration = {
project: project,
invited_by: inviter._id,
access_level: accessLevel
};
InviteCode.findOne({emailed_to: email, consumed_timestamp: null}, function (err, invite) {
if (err) return done(err);
if (invite) {
return updateInvite(invite, collaboration, done);
}
sendInvite(inviter, email, collaboration, done);
});
}
});
}
function del(project, email, done) {
User.update({email: email, 'projects.name': project.toLowerCase()},
{$pull: {'projects': {'name': project.toLowerCase()}}}, done);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var express = require('express');
var middleware = require('../../middleware');
var auth = require('../../auth');
var models = require('../../models');
var utils = require('../../utils');
var api = require('./api');
var router = new express.Router();
var User = models.User;
router.route('/:org/:repo/collaborators/')
.all(
auth.requireUserOr401,
middleware.project,
auth.requireProjectAdmin
)
/**
* @api {get} /:org/:repo/collaborators Get Collaborators
* @apiUse ProjectReference
* @apiDescription Gets a list of collaborators for a project
* @apiName GetCollaborators
* @apiGroup Collaborators
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET http://localhost/api/strider-cd/strider/collaborators
*/
.get(function getCollab(req, res) {
var project = `${req.params.org}/${req.params.repo}`;
User.collaborators(project, 0, function (err, users) {
if (err) return res.status(500).send(`Failed to get users: ${err.message}`);
var results = [];
for (var i = 0; i < users.length; i++) {
var p = _.find(users[i].projects, function (p) {
return p.name === project.toLowerCase();
});
results.push({
type: 'user',
id: users[i]._id,
email: users[i].email,
access_level: p.access_level,
gravatar: utils.gravatar(users[i].email)
});
}
res.send(results);
});
})
/**
* @api {post} /:org/:repo/collaborators Add Collaborator
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Add a new collaborator to a repository/project.
* @apiName AddCollaborator
* @apiGroup Collaborators
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X GET -d '{"email":"new_guy@strider-cd.com", "access":2}' http://localhost/api/strider-cd/strider/collaborators
*
* @apiParam (RequestBody) {String} email Email address to add. If the user is
* not registered with Strider, we will send them an invite. If they are already
* registered, they will receive a notification of access.
* @apiParam (RequestBody) {Number} access=0 Access level to grant to the
* new collaborator. This can be `0`, for read only access, or `2` for admin access.
*/
.post(middleware.requireBody(['email']), function addCollab(req, res) {
var project = `${req.params.org}/${req.params.repo}`;
var accessLevel = req.body.access || 0;
var email = req.body.email;
api.add(project, email, accessLevel, req.user, function (err, existed, alreadyInvited) {
if (err) return res.status(500).send(`Failed to add collaborator: ${err.message}`);
if (existed) return res.send({created: true, message: 'Collaborator added'});
if (!alreadyInvited) return res.send({
created: false,
message: `An invite was sent to ${email}. They will become a collaborator when they create an account.`
});
res.send({
created: false,
message: `An invitation email has already been sent to ${email}. They will become a collaborator when they create an account.`
});
});
})
/**
* @api {delete} /:org/:repo/collaborators Delete Collaborator
* @apiUse ProjectReference
* @apiPermission ProjectAdmin
* @apiDescription Remove a collaborator from a repository/project.
* @apiName DeleteCollaborator
* @apiGroup Collaborators
* @apiVersion 1.0.0
*
* @apiExample {curl} CURL Example:
* curl -X DELETE -d '{"email":"old_guy@strider-cd.com"}' http://localhost/api/strider-cd/strider/collaborators
*
* @apiParam (RequestBody) {String} email Email address to remove from the repo/project.
*/
.delete(middleware.requireBody(['email']), function delCollab(req, res) {
var project = `${req.params.org}/${req.params.repo}`;
var email = req.body.email;
if (req.project.creator.email.toLowerCase() === email.toLowerCase()) {
return res.status(400).send('Cannot remove the project creator');
}
if (req.user.email.toLowerCase() === email.toLowerCase()) {
return res.status(400).send('Cannot remove yourself');
}
api.del(project, email, function (err) {
if (err) return res.status(500).send(err.message);
res.send({status: 'removed'});
});
});
module.exports = router;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 18% | (18 / 100) | 0% | (0 / 45) | 0% | (0 / 19) | 19.15% | (18 / 94) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
/*
* routes/jobs/index.js
*/
var _ = require('lodash');
var common = require('../../common');
var debug = require('debug')('strider:routes:jobs');
var filter = require('../../ansi');
var ljobs = require('../../jobs');
var models = require('../../models');
var pjson = require('../../../package.json');
var utils = require('../../utils');
var Job = models.Job;
module.exports = {
html: html,
multijob: multijob,
jobs: jobs
};
/*
* GET /org/repo/[job/:job_id] - view latest build for repo
*
* middleware.project set "project" and "accessLevel" on the req object.
*/
function multijob(req, res) {
var type = req.accepts('html', 'json', 'plain');
switch (type) {
case 'json':
return data(req, res);
case 'plain':
return output(req, res);
default:
return html(req, res);
}
}
function filterJob(job) {
if (job.trigger.message === 'Retest') {
job.trigger.message = 'Manually Retested';
}
if (job.trigger.message === 'Redeploy') {
job.trigger.message = 'Manually Redeployed';
}
return job;
}
function findJob(job) {
// job.runner can be undefined if it hasn't been fully prepared yet.
// this is a sort of race between job.prepare and job.new events.
// fixes https://github.com/Strider-CD/strider/issues/273
if (!job.runner) return;
var runner = common.extensions.runner[job.runner.id];
if (runner) return runner.getJobData(job._id) || {};
}
function html(req, res, next) {
if (req.params.org === 'auth') {
return next();
}
var id = req.params.id;
var projectName = req.project.name;
Job.find({project: projectName, archived: null}).sort({finished: -1}).limit(20).lean().exec(function (err, jobs) {
if (err) {
debug('[job] error finding jobs', err.message);
return res.status(500).send('Failed to find jobs');
}
// Use our custom sort function
jobs.sort(ljobs.sort);
Job.find({
project: projectName,
archived: null,
finished: null
}).sort({started: -1}).lean().exec(function (err, running) {
if (err) {
debug('[job] error finding running jobs', err.message);
return res.status(500).send('Failed to find running jobs');
}
var i;
for (i = 0; i < running.length; i++) {
_.extend(running[i], findJob(running[i]));
delete running[i].data;
delete running[i].id;
}
jobs = running.concat(jobs);
var showStatus = {};
var sanitized = utils.sanitizeProject(req.project);
sanitized.access_level = req.accessLevel;
req.project.branches.forEach(function (branch) {
var plugins = showStatus[branch.name] = {};
branch.plugins.forEach(function (plugin) {
plugins[plugin.id] = plugin.enabled && plugin.showStatus;
});
});
var job = id ? null : jobs[0];
for (i = 0; i < jobs.length; i++) {
if (!job && jobs[i]._id === id) job = jobs[i];
jobs[i] = ljobs.small(jobs[i]);
jobs[i] = filterJob(jobs[i]);
jobs[i].project = sanitized;
}
if (job) {
job.status = ljobs.status(job);
job.project = sanitized;
}
var isGlobalAdmin = req.user && req.user.account_level > 0;
var canAdminProject = sanitized.access_level > 0 || isGlobalAdmin;
// Make sure jobs are only listed once.
jobs = _.uniqBy(jobs, job => job._id.toString());
res.format({
html: function () {
debug('Build page requested. Logging jobs to investigate duplicate job listings.');
debug(jobs);
res.render('build.html', {
project: sanitized,
accessLevel: req.accessLevel,
canAdminProject: canAdminProject,
jobs: jobs,
job: job,
statusBlocks: common.statusBlocks,
showStatus: showStatus,
page_base: `${req.params.org}/${req.params.repo}`,
version: pjson.version
});
},
json: function () {
res.send({
project: sanitized,
accessLevel: req.accessLevel,
canAdminProject: canAdminProject,
jobs: jobs,
job: job
});
}
});
});
});
}
function getJob(req, res, next) {
var query;
if (!req.params.job_id) {
query = Job.findOne({project: req.project.name.toLowerCase(), archived: null}, {}, {sort: {finished: -1}});
} else {
query = Job.findOne({project: req.project.name.toLowerCase(), _id: req.params.job_id, archived: null});
}
query.exec(function (err, job) {
if (err || !job) return res.status(404).send('Failed to find job');
job = filterJob(job);
if (!job.finished) {
_.extend(job, findJob(job));
}
next(job);
});
}
function output(req, res) {
getJob(req, res, function (job) {
res.setHeader('Content-type', 'text/plain');
res.send(job.std.merged ? filter(job.std.merged) : '');
});
}
function data(req, res) {
getJob(req, res, function (job) {
res.setHeader('Content-type', 'application/json');
res.send(job);
});
}
function jobs(req, res) {
Job.find({project: req.project.name.toLowerCase(), archived: null})
.sort({finished: -1}).limit(20).lean()
.exec(function (err, jobs) {
if (err) return res.status(500).send('Failed to retrieve jobs');
res.send(JSON.stringify(jobs.map(function (j) {
return filterJob(j);
})));
});
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 13.28% | (17 / 128) | 0% | (0 / 76) | 0% | (0 / 14) | 15.89% | (17 / 107) | |
| kill-zombies.js | 33.33% | (3 / 9) | 0% | (0 / 2) | 0% | (0 / 2) | 37.5% | (3 / 8) | |
| load-extensions.js | 21.43% | (9 / 42) | 0% | (0 / 8) | 0% | (0 / 10) | 23.08% | (9 / 39) | |
| register-panel.js | 18.18% | (2 / 11) | 0% | (0 / 4) | 0% | (0 / 2) | 18.18% | (2 / 11) | |
| require-body.js | 33.33% | (2 / 6) | 0% | (0 / 2) | 0% | (0 / 1) | 33.33% | (2 / 6) | |
| user-socket.js | 13.64% | (15 / 110) | 0% | (0 / 44) | 0% | (0 / 31) | 15.15% | (15 / 99) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var _ = require('lodash');
var common = require('../../lib/common');
var debug = require('debug')('strider:utils');
var gravatar = require('gravatar');
module.exports = {
gravatar: gravatarDefault,
sanitizeProject: sanitizeProject,
sanitizeBranch: sanitizeBranch,
sanitizeUser: sanitizeUser,
timeFromId: timeFromId,
defaultSchema: defaultSchema,
validateAgainstSchema: validateAgainstSchema,
mergePlugins: mergePlugins,
mergeConfigs: mergeConfigs,
findBranch: findBranch
};
function findBranch(branches, name) {
var foundBranch = false;
branches.some(function (branch) {
if (branch.name) {
var regEx = new RegExp(`^${branch.name.replace(/\*/g, '.*')}$`);
if (regEx.test(name)) {
foundBranch = branch;
return true;
}
}
});
return (function discreteBranchFn(name, branch, branches) {
//console.log('findBranch('+name+') wants to return '+branch.name);
if (branch.name !== name) {
//console.warn('Possibly returning unintended branch (expected '+name+' but got '+branch.name+'). attempting to locate discretely named branch '+name+' if it exists.');
var discreteBranch = _.find(branches, {name: name});
if (discreteBranch) {
branch = discreteBranch;
//console.info("Located discrete branch, instead returning "+branch.name)
} else {
//console.warn("Unable to find discrete branch "+name+", still returning "+branch.name);
}
}
return branch;
}(name, foundBranch, branches));
}
// merge plugins from the DB with ones from strider.json. The latter overrides the former
function mergePlugins(branch, sjson) {
if (!branch) return sjson;
if (!sjson) return branch;
// if strict_plugins is not turned on, we merge each plugin config instead of overwriting.
var plugins = [];
var pluginMap = {};
for (var pluginIndex = 0; pluginIndex < sjson.length; pluginIndex++) {
plugins.push(sjson[pluginIndex]);
pluginMap[sjson[pluginIndex].id] = true;
}
for (var branchIndex = 0; branchIndex < branch.length; branchIndex++) {
if (!pluginMap[branch[branchIndex].id]) plugins.push(branch[branchIndex]);
}
return plugins;
}
function mergeConfigs(branch, striderJson) {
// Is this still a mongoose document?
if (typeof branch.toObject === 'function') {
branch = branch.toObject();
}
// Copy all properties of the branch configuration to a new object (inherited properties are ignored, there
// shouldn't be any anyway). Then override all values with what is defined in the strider.json.
// Note that this overrides all plugin configuration.
const config = _.assign({}, branch, striderJson);
// Get a list of all plugins and their default configuration schemas.
// We can find these in common.extensions, which contains separate objects for every type of extension that exists.
// In those objects we find keys which are the IDs of the plugin and the value is the configuration schema of that
// plugin.
// We recursively traverse this data structure and merge it into a single hash, while generating an instance of the
// default configuration, so we can access the configurations for each extension easily.
const allSchemas = _.keys(common.extensions).reduce((allSchemas, extensionType) => {
_.keys(common.extensions[extensionType]).reduce((allSchemas, pluginId) => {
if(extensionType === 'runner') return allSchemas;
allSchemas[pluginId] = defaultSchema(common.extensions[extensionType][pluginId]);
return allSchemas;
}, allSchemas);
return allSchemas;
}, {});
// For every plugin, merge the default configuration into a new object and then assign the user configuration
// over it.
if(config.plugins && config.plugins.length) {
config.plugins.forEach(plugin => {
if(!allSchemas[plugin.id]) return plugin.config;
plugin.config = _.assign({}, allSchemas[plugin.id], plugin.config);
});
}
// If the user requests to have plugin configurations merged (through the merge_plugins setting in the strider.json)
// merge the plugin configurations.
// Here, we do not need to care about the default configuration of a plugin, as the web UI will always have the
// full configuration schema populated.
if (!striderJson.merge_plugins) return config;
debug('Merging plugin configurations');
config.plugins = mergePlugins(branch.plugins, striderJson.plugins);
return config;
}
function validateVal(val, schema) {
if (schema === String) return `${val}`;
if (schema === Number) {
val = parseFloat(schema);
return isNaN(val) ? 0 : val;
}
if (schema === Boolean) return !!val;
if (Array.isArray(schema)) {
var ret = [];
if (!Array.isArray(val)) return [];
for (var i = 0; i < val.length; i++) {
ret.push(validateVal(val[i], schema[0]));
}
return ret;
}
if (schema.type && !schema.type.type) {
val = validateVal(val, schema.type);
if (schema.enum && schema.enum.indexOf(val) === -1) {
return;
}
return val;
}
if ('object' !== typeof schema) return;
if ('object' !== typeof val) return {};
// if schema is {}, it's unchecked.
if (Object.keys(schema).length === 0) return val;
return validateAgainstSchema(val, schema);
}
function defaultVal(val) {
if (val === String) return '';
if (val === Number) return 0;
if (val === Boolean) return false;
if (!val) return null;
if (Array.isArray(val)) return [];
if (val.type && !val.type.type) {
if (val.default) return val.default;
if (val.enum) return val.enum[0];
return defaultVal(val.type);
}
if ('object' === typeof val) return defaultSchema(val);
return null;
}
function defaultSchema(schema) {
var data = {};
for (var key in schema) {
data[key] = defaultVal(schema[key]);
}
return data;
}
function validateAgainstSchema(obj, schema) {
var data = {};
for (var key in obj) {
if (!schema[key]) continue;
data[key] = validateVal(obj[key], schema[key]);
}
return data;
}
function timeFromId(id) {
return new Date(parseInt(id.toString().substring(0, 8), 16) * 1000);
}
function sanitizeBranch(branch) {
var plugins = [];
for (var i = 0; i < branch.plugins; i++) {
plugins.push({id: branch.plugins[i].id, enabled: branch.plugins[i].enabled});
}
return {
plugins: plugins,
public: branch.public,
active: branch.active,
deploy_on_green: branch.deploy_on_green,
deploy_on_pull_request: branch.deploy_on_pull_request,
runner: {
id: branch.runner && branch.runner.id
}
};
}
function sanitizeUser(user) {
for (var i = 0; i < user.accounts.length; i++) {
delete user.accounts[i].cache;
}
return user;
}
function sanitizeProject(project) {
return {
_id: project._id,
name: project.name,
branches: project.branches.map(sanitizeBranch),
public: project.public,
display_url: project.display_url,
display_name: project.display_name,
provider: {
id: project.provider.id
}
};
}
function gravatarDefault(email) {
return gravatar.url(email, {
d: 'identicon'
}, true);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 1 1 1 | 'use strict';
var debug = require('debug')('strider:kill-zombies');
var Job = require('../models/job');
module.exports = function killZombies(done) {
debug('Marking zombie jobs as finished...');
Job.update({
archived: null,
finished: null
}, {
$set: {
finished: new Date(),
errored: true
}
}, {
multi: true
}, function (err, count) {
if (err) throw err;
debug('%d zombie jobs marked as finished', count.nModified);
done();
}
);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1 1 1 1 1 1 1 1 1 | 'use strict';
var apiConfig = require('../routes/api/config');
var app = require('../app');
var async = require('async');
var common = require('../common');
var debug = require('debug')('strider:load-extensions');
var pluginTemplates = require('../plugin-templates');
var slashes = require('connect-slashes');
function loadExtensions(loader, extdir, context, appInstance, cb) {
loader.collectExtensions(extdir, function (err) {
if (err) return cb(err);
async.parallel([
function (next) {
loader.initWebAppExtensions(context, function (err, webapps) {
if (err) return next(err);
common.extensions = webapps;
debug('initalized webapps');
for (var type in webapps) {
debug(`Found ${type} plugins:`);
for (var id in webapps[type]) {
debug(` - ${id}`);
}
}
next();
});
},
function (next) {
loader.initTemplates(function (err, templates) {
if (err) return next(err);
for (var name in templates) {
pluginTemplates.registerTemplate(name, templates[name]);
}
debug('loaded templates');
next();
});
},
function (next) {
loader.initStaticDirs(appInstance, function () {
debug('initialized static directories');
next();
});
},
function (next) {
apiConfig.cacheConfig(loader, next);
}
], function (err) {
if (err) {
debug(err);
debug('Failed to load plugins');
return cb(err, appInstance);
}
debug('loaded plugins');
appInstance.use(slashes(true, true));
app.run(appInstance);
cb(null, appInstance);
});
});
}
module.exports = loadExtensions;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 1 | 'use strict';
var debug = require('debug')('strider:register-panel');
// ### Register panel
//
// A panel is simply a snippet of HTML associated with a given key.
// Strider will output panels registered for specific template.
//
module.exports = function setupRegisterPanel(common) {
return function registerPanel(key, value) {
// Nothing yet registered for this panel
key = value.id;
debug('!! registerPanel', key);
if (common.extensions[key] === undefined) {
common.extensions[key] = { panel: value };
} else {
if (common.extensions[key].panel) {
debug('!!', key, common.extensions[key], value);
throw `Multiple Panels for ${key}`;
}
common.extensions[key].panel = value;
}
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 1 1 | 'use strict'; /* * Require a request parameter is present or return a 400 response. */ function requireBody(key, req, res) { var val = req.body[key]; if (val === undefined) { return res.status(400).json({ status: 'error', errors: [`you must supply parameter ${key}`] }); } return val; } module.exports = requireBody; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 'use strict';
var gravatar = require('gravatar');
var models = require('../models');
var common = require('../common');
var jobs = require('../jobs');
var utils = require('../utils');
var Project = models.Project;
var User = models.User;
var Job = models.Job;
function UserSocket(userid) {
this.userid = userid;
this.sockets = [];
}
module.exports = UserSocket;
UserSocket.prototype = {
add: function (socket) {
var waiters = {};
var self = this;
for (var name in this.events) {
socket.on(name, (waiters[name] = waiter(socket, name)).handler);
}
this.getUser(function () {
for (var name in self.events) {
unWait(socket, waiters[name], self.events[name].bind(self));
}
});
this.sockets.push(socket);
},
remove: function (socket) {
var idx = this.sockets.indexOf(socket);
if (idx === -1) return false;
this.sockets.splice(idx, 1);
return true;
},
/**
* Emit an even on all sockets
*/
emit: function (args) {
this.sockets.forEach(function (socket) {
socket.emit.apply(socket, args);
});
},
/**
* Apply handlers for all sockets
*/
on: function () {
this.sockets.forEach(function (socket) {
socket.on.apply(socket, arguments);
});
},
getUser: function (done) {
if (!this.userid) return done();
var self = this;
User.findById(this.userid, function (err, user) {
if (err) console.error('failed to get user - socket', err);
if (!user) {
console.error('user not found for the websocket - there\'s something strange going on w/ websocket auth. User Id: ', self.userid);
self.emit(['auth.failed']);
return;
}
self.user = user;
done();
});
},
events: {
'dashboard:jobs': function (done) {
jobs.latestJobs(this.user, function (err, jobs) {
done(jobs);
});
},
'dashboard:unknown': function (id, done) {
var user = this.user;
Job.findById(id).lean().exec(function (err, job) {
if (err || !job) return console.error('[unknownjob] error getting job', id, err);
Project.findOne({name: job.project.toLowerCase()}).lean().exec(function (err, project) {
if (err || !project) return console.error('[unknownjob] error getting project', id, err);
var njob = jobs.small(job);
njob.project = utils.sanitizeProject(project);
njob.project.access_level = User.projectAccessLevel(user, project);
// this will be filled in
njob.project.prev = [];
done(njob);
});
});
},
'build:job': function (id, done) {
var user = this.user;
Job.findById(id).lean().exec(function (err, job) {
if (err) return console.error('Error retrieving job', id, err.message, err.stack);
if (!job) return console.error('Job not found', id);
Project.findOne({name: job.project.toLowerCase()}).lean().exec(function (err, project) {
if (err || !project) return console.error('Error getting project', job.project, err);
job.status = jobs.status(job);
job.project = utils.sanitizeProject(project);
job.project.access_level = User.projectAccessLevel(user, project);
job.project.prev = [];
done(job);
});
});
},
'build:unknown': function () {
// TODO: query the responsible runner to get the current output, etc.
},
'test': function (project, branch) {
var user = this.user;
startJob(project, user, branch, 'TEST_ONLY');
},
'deploy': function (project, branch) {
var user = this.user;
startJob(project, user, branch, 'TEST_AND_DEPLOY');
},
'cancel': function (id) {
console.log('Got a request to cancel', id);
var self = this;
Job.findById(id).lean().exec(function (err, job) {
if (err || !job) return console.error('[canceljob] error getting job', id, err);
Project.findOne({name: job.project.toLowerCase()}).lean().exec(function (err, project) {
if (err || !project) return console.error('[canceljob] error getting project', id, err);
if (!job) return console.error('[canceljob] Job not found', id);
if (User.projectAccessLevel(self.user, project) > 0) {
common.emitter.emit('job.cancel', id);
}
});
});
},
'restart': function () {
console.log('Implementation needed');
}
}
};
function waiter(socket, event) {
var wait = {
event: event,
calls: [],
handler: function () {
wait.calls.push(arguments);
}
};
return wait;
}
function unWait(socket, waiter, handler) {
socket.removeListener(waiter.event, waiter.handler);
socket.on(waiter.event, handler);
for (var i = 0; i < waiter.calls.length; i++) {
handler.apply(null, waiter.calls[i]);
}
}
function kickoffJob(user, project, type, branch) {
var now = new Date();
var trigger;
var job;
branch = branch || 'master';
trigger = {
type: 'manual',
author: {
id: user._id,
email: user.email,
image: gravatar.url(user.email, {}, true)
},
message: type === 'TEST_AND_DEPLOY' ? 'Manually Redeploying' : 'Manually Retesting',
timestamp: now,
source: {type: 'UI', page: 'unknown'}
};
if (branch !== 'master') {
trigger.message += ` ${branch}`;
}
job = {
type: type,
user_id: user._id,
project: project,
ref: {branch: branch},
trigger: trigger,
created: now
};
common.emitter.emit('job.prepare', job);
}
function startJob(projectName, user, branch, jobType) {
Project.findOne({name: projectName}).lean().exec(function (err, project) {
if (User.projectAccessLevel(user, project) > 0) {
kickoffJob(user, project.name, jobType, branch);
}
});
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| ui-bootstrap.js | 4.49% | (29 / 646) | 0% | (0 / 356) | 0% | (0 / 161) | 4.49% | (29 / 646) | |
| ui-codemirror.js | 1.89% | (1 / 53) | 0% | (0 / 34) | 0% | (0 / 11) | 1.89% | (1 / 53) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 | 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | angular.module("ui.bootstrap", ["ui.bootstrap.buttons","ui.bootstrap.position","ui.bootstrap.datepicker","ui.bootstrap.pagination","ui.bootstrap.rating","ui.bootstrap.timepicker","ui.bootstrap.bindHtml","ui.bootstrap.typeahead"]);
angular.module('ui.bootstrap.buttons', [])
.constant('buttonConfig', {
activeClass:'active',
toggleEvent:'click'
})
.directive('btnRadio', ['buttonConfig', function (buttonConfig) {
var activeClass = buttonConfig.activeClass || 'active';
var toggleEvent = buttonConfig.toggleEvent || 'click';
return {
require:'ngModel',
link:function (scope, element, attrs, ngModelCtrl) {
//model -> UI
ngModelCtrl.$render = function () {
element.toggleClass(activeClass, angular.equals(ngModelCtrl.$modelValue, scope.$eval(attrs.btnRadio)));
};
//ui->model
element.bind(toggleEvent, function () {
if (!element.hasClass(activeClass)) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(scope.$eval(attrs.btnRadio));
ngModelCtrl.$render();
});
}
});
}
};
}])
.directive('btnCheckbox', ['buttonConfig', function (buttonConfig) {
var activeClass = buttonConfig.activeClass || 'active';
var toggleEvent = buttonConfig.toggleEvent || 'click';
return {
require:'ngModel',
link:function (scope, element, attrs, ngModelCtrl) {
function getTrueValue() {
var trueValue = scope.$eval(attrs.btnCheckboxTrue);
return angular.isDefined(trueValue) ? trueValue : true;
}
function getFalseValue() {
var falseValue = scope.$eval(attrs.btnCheckboxFalse);
return angular.isDefined(falseValue) ? falseValue : false;
}
//model -> UI
ngModelCtrl.$render = function () {
element.toggleClass(activeClass, angular.equals(ngModelCtrl.$modelValue, getTrueValue()));
};
//ui->model
element.bind(toggleEvent, function () {
scope.$apply(function () {
ngModelCtrl.$setViewValue(element.hasClass(activeClass) ? getFalseValue() : getTrueValue());
ngModelCtrl.$render();
});
});
}
};
}]);
angular.module('ui.bootstrap.position', [])
/**
* A set of utility methods that can be use to retrieve position of DOM elements.
* It is meant to be used where we need to absolute-position DOM elements in
* relation to other, existing elements (this is the case for tooltips, popovers,
* typeahead suggestions etc.).
*/
.factory('$position', ['$document', '$window', function ($document, $window) {
function getStyle(el, cssprop) {
if (el.currentStyle) { //IE
return el.currentStyle[cssprop];
} else if ($window.getComputedStyle) {
return $window.getComputedStyle(el)[cssprop];
}
// finally try and get inline style
return el.style[cssprop];
}
/**
* Checks if a given element is statically positioned
* @param element - raw DOM element
*/
function isStaticPositioned(element) {
return (getStyle(element, "position") || 'static' ) === 'static';
}
/**
* returns the closest, non-statically positioned parentOffset of a given element
* @param element
*/
var parentOffsetEl = function (element) {
var docDomEl = $document[0];
var offsetParent = element.offsetParent || docDomEl;
while (offsetParent && offsetParent !== docDomEl && isStaticPositioned(offsetParent) ) {
offsetParent = offsetParent.offsetParent;
}
return offsetParent || docDomEl;
};
return {
/**
* Provides read-only equivalent of jQuery's position function:
* http://api.jquery.com/position/
*/
position: function (element) {
var elBCR = this.offset(element);
var offsetParentBCR = { top: 0, left: 0 };
var offsetParentEl = parentOffsetEl(element[0]);
if (offsetParentEl != $document[0]) {
offsetParentBCR = this.offset(angular.element(offsetParentEl));
offsetParentBCR.top += offsetParentEl.clientTop - offsetParentEl.scrollTop;
offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
}
return {
width: element.prop('offsetWidth'),
height: element.prop('offsetHeight'),
top: elBCR.top - offsetParentBCR.top,
left: elBCR.left - offsetParentBCR.left
};
},
/**
* Provides read-only equivalent of jQuery's offset function:
* http://api.jquery.com/offset/
*/
offset: function (element) {
var boundingClientRect = element[0].getBoundingClientRect();
return {
width: element.prop('offsetWidth'),
height: element.prop('offsetHeight'),
top: boundingClientRect.top + ($window.pageYOffset || $document[0].body.scrollTop || $document[0].documentElement.scrollTop),
left: boundingClientRect.left + ($window.pageXOffset || $document[0].body.scrollLeft || $document[0].documentElement.scrollLeft)
};
}
};
}]);
angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.position'])
.constant('datepickerConfig', {
dayFormat: 'dd',
monthFormat: 'MMMM',
yearFormat: 'yyyy',
dayHeaderFormat: 'EEE',
dayTitleFormat: 'MMMM yyyy',
monthTitleFormat: 'yyyy',
showWeeks: true,
startingDay: 0,
yearRange: 20,
minDate: null,
maxDate: null
})
.controller('DatepickerController', ['$scope', '$attrs', 'dateFilter', 'datepickerConfig', function($scope, $attrs, dateFilter, dtConfig) {
var format = {
day: getValue($attrs.dayFormat, dtConfig.dayFormat),
month: getValue($attrs.monthFormat, dtConfig.monthFormat),
year: getValue($attrs.yearFormat, dtConfig.yearFormat),
dayHeader: getValue($attrs.dayHeaderFormat, dtConfig.dayHeaderFormat),
dayTitle: getValue($attrs.dayTitleFormat, dtConfig.dayTitleFormat),
monthTitle: getValue($attrs.monthTitleFormat, dtConfig.monthTitleFormat)
},
startingDay = getValue($attrs.startingDay, dtConfig.startingDay),
yearRange = getValue($attrs.yearRange, dtConfig.yearRange);
this.minDate = dtConfig.minDate ? new Date(dtConfig.minDate) : null;
this.maxDate = dtConfig.maxDate ? new Date(dtConfig.maxDate) : null;
function getValue(value, defaultValue) {
return angular.isDefined(value) ? $scope.$parent.$eval(value) : defaultValue;
}
function getDaysInMonth( year, month ) {
return new Date(year, month, 0).getDate();
}
function getDates(startDate, n) {
var dates = new Array(n);
var current = startDate, i = 0;
while (i < n) {
dates[i++] = new Date(current);
current.setDate( current.getDate() + 1 );
}
return dates;
}
function makeDate(date, format, isSelected, isSecondary) {
return { date: date, label: dateFilter(date, format), selected: !!isSelected, secondary: !!isSecondary };
}
this.modes = [
{
name: 'day',
getVisibleDates: function(date, selected) {
var year = date.getFullYear(), month = date.getMonth(), firstDayOfMonth = new Date(year, month, 1);
var difference = startingDay - firstDayOfMonth.getDay(),
numDisplayedFromPreviousMonth = (difference > 0) ? 7 - difference : - difference,
firstDate = new Date(firstDayOfMonth), numDates = 0;
if ( numDisplayedFromPreviousMonth > 0 ) {
firstDate.setDate( - numDisplayedFromPreviousMonth + 1 );
numDates += numDisplayedFromPreviousMonth; // Previous
}
numDates += getDaysInMonth(year, month + 1); // Current
numDates += (7 - numDates % 7) % 7; // Next
var days = getDates(firstDate, numDates), labels = new Array(7);
for (var i = 0; i < numDates; i ++) {
var dt = new Date(days[i]);
days[i] = makeDate(dt, format.day, (selected && selected.getDate() === dt.getDate() && selected.getMonth() === dt.getMonth() && selected.getFullYear() === dt.getFullYear()), dt.getMonth() !== month);
}
for (var j = 0; j < 7; j++) {
labels[j] = dateFilter(days[j].date, format.dayHeader);
}
return { objects: days, title: dateFilter(date, format.dayTitle), labels: labels };
},
compare: function(date1, date2) {
return (new Date( date1.getFullYear(), date1.getMonth(), date1.getDate() ) - new Date( date2.getFullYear(), date2.getMonth(), date2.getDate() ) );
},
split: 7,
step: { months: 1 }
},
{
name: 'month',
getVisibleDates: function(date, selected) {
var months = new Array(12), year = date.getFullYear();
for ( var i = 0; i < 12; i++ ) {
var dt = new Date(year, i, 1);
months[i] = makeDate(dt, format.month, (selected && selected.getMonth() === i && selected.getFullYear() === year));
}
return { objects: months, title: dateFilter(date, format.monthTitle) };
},
compare: function(date1, date2) {
return new Date( date1.getFullYear(), date1.getMonth() ) - new Date( date2.getFullYear(), date2.getMonth() );
},
split: 3,
step: { years: 1 }
},
{
name: 'year',
getVisibleDates: function(date, selected) {
var years = new Array(yearRange), year = date.getFullYear(), startYear = parseInt((year - 1) / yearRange, 10) * yearRange + 1;
for ( var i = 0; i < yearRange; i++ ) {
var dt = new Date(startYear + i, 0, 1);
years[i] = makeDate(dt, format.year, (selected && selected.getFullYear() === dt.getFullYear()));
}
return { objects: years, title: [years[0].label, years[yearRange - 1].label].join(' - ') };
},
compare: function(date1, date2) {
return date1.getFullYear() - date2.getFullYear();
},
split: 5,
step: { years: yearRange }
}
];
this.isDisabled = function(date, mode) {
var currentMode = this.modes[mode || 0];
return ((this.minDate && currentMode.compare(date, this.minDate) < 0) || (this.maxDate && currentMode.compare(date, this.maxDate) > 0) || ($scope.dateDisabled && $scope.dateDisabled({date: date, mode: currentMode.name})));
};
}])
.directive( 'datepicker', ['dateFilter', '$parse', 'datepickerConfig', '$log', function (dateFilter, $parse, datepickerConfig, $log) {
return {
restrict: 'EA',
replace: true,
templateUrl: 'template/datepicker/datepicker.html',
scope: {
dateDisabled: '&'
},
require: ['datepicker', '?^ngModel'],
controller: 'DatepickerController',
link: function(scope, element, attrs, ctrls) {
var datepickerCtrl = ctrls[0], ngModel = ctrls[1];
if (!ngModel) {
return; // do nothing if no ng-model
}
// Configuration parameters
var mode = 0, selected = new Date(), showWeeks = datepickerConfig.showWeeks;
if (attrs.showWeeks) {
scope.$parent.$watch($parse(attrs.showWeeks), function(value) {
showWeeks = !! value;
updateShowWeekNumbers();
});
} else {
updateShowWeekNumbers();
}
if (attrs.min) {
scope.$parent.$watch($parse(attrs.min), function(value) {
datepickerCtrl.minDate = value ? new Date(value) : null;
refill();
});
}
if (attrs.max) {
scope.$parent.$watch($parse(attrs.max), function(value) {
datepickerCtrl.maxDate = value ? new Date(value) : null;
refill();
});
}
function updateShowWeekNumbers() {
scope.showWeekNumbers = mode === 0 && showWeeks;
}
// Split array into smaller arrays
function split(arr, size) {
var arrays = [];
while (arr.length > 0) {
arrays.push(arr.splice(0, size));
}
return arrays;
}
function refill( updateSelected ) {
var date = null, valid = true;
if ( ngModel.$modelValue ) {
date = new Date( ngModel.$modelValue );
if ( isNaN(date) ) {
valid = false;
$log.error('Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
} else if ( updateSelected ) {
selected = date;
}
}
ngModel.$setValidity('date', valid);
var currentMode = datepickerCtrl.modes[mode], data = currentMode.getVisibleDates(selected, date);
angular.forEach(data.objects, function(obj) {
obj.disabled = datepickerCtrl.isDisabled(obj.date, mode);
});
ngModel.$setValidity('date-disabled', (!date || !datepickerCtrl.isDisabled(date)));
scope.rows = split(data.objects, currentMode.split);
scope.labels = data.labels || [];
scope.title = data.title;
}
function setMode(value) {
mode = value;
updateShowWeekNumbers();
refill();
}
ngModel.$render = function() {
refill( true );
};
scope.select = function( date ) {
if ( mode === 0 ) {
var dt = new Date( ngModel.$modelValue );
dt.setFullYear( date.getFullYear(), date.getMonth(), date.getDate() );
ngModel.$setViewValue( dt );
refill( true );
} else {
selected = date;
setMode( mode - 1 );
}
};
scope.move = function(direction) {
var step = datepickerCtrl.modes[mode].step;
selected.setMonth( selected.getMonth() + direction * (step.months || 0) );
selected.setFullYear( selected.getFullYear() + direction * (step.years || 0) );
refill();
};
scope.toggleMode = function() {
setMode( (mode + 1) % datepickerCtrl.modes.length );
};
scope.getWeekNumber = function(row) {
return ( mode === 0 && scope.showWeekNumbers && row.length === 7 ) ? getISO8601WeekNumber(row[0].date) : null;
};
function getISO8601WeekNumber(date) {
var checkDate = new Date(date);
checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7)); // Thursday
var time = checkDate.getTime();
checkDate.setMonth(0); // Compare with Jan 1
checkDate.setDate(1);
return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
}
}
};
}])
.constant('datepickerPopupConfig', {
dateFormat: 'yyyy-MM-dd',
closeOnDateSelection: true
})
.directive('datepickerPopup', ['$compile', '$parse', '$document', '$position', 'dateFilter', 'datepickerPopupConfig',
function ($compile, $parse, $document, $position, dateFilter, datepickerPopupConfig) {
return {
restrict: 'EA',
require: 'ngModel',
link: function(originalScope, element, attrs, ngModel) {
var closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? scope.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection;
var dateFormat = attrs.datepickerPopup || datepickerPopupConfig.dateFormat;
// create a child scope for the datepicker directive so we are not polluting original scope
var scope = originalScope.$new();
originalScope.$on('$destroy', function() {
scope.$destroy();
});
var getIsOpen, setIsOpen;
if ( attrs.isOpen ) {
getIsOpen = $parse(attrs.isOpen);
setIsOpen = getIsOpen.assign;
originalScope.$watch(getIsOpen, function updateOpen(value) {
scope.isOpen = !! value;
});
}
scope.isOpen = getIsOpen ? getIsOpen(originalScope) : false; // Initial state
function setOpen( value ) {
if (setIsOpen) {
setIsOpen(originalScope, !!value);
} else {
scope.isOpen = !!value;
}
}
var documentClickBind = function(event) {
if (scope.isOpen && event.target !== element[0]) {
scope.$apply(function() {
setOpen(false);
});
}
};
var elementFocusBind = function() {
scope.$apply(function() {
setOpen( true );
});
};
// popup element used to display calendar
var popupEl = angular.element('<datepicker-popup-wrap><datepicker></datepicker></datepicker-popup-wrap>');
popupEl.attr({
'ng-model': 'date',
'ng-change': 'dateSelection()'
});
var datepickerEl = popupEl.find('datepicker');
if (attrs.datepickerOptions) {
datepickerEl.attr(angular.extend({}, originalScope.$eval(attrs.datepickerOptions)));
}
// TODO: reverse from dateFilter string to Date object
function parseDate(viewValue) {
if (!viewValue) {
ngModel.$setValidity('date', true);
return null;
} else if (angular.isDate(viewValue)) {
ngModel.$setValidity('date', true);
return viewValue;
} else if (angular.isString(viewValue)) {
var date = new Date(viewValue);
if (isNaN(date)) {
ngModel.$setValidity('date', false);
return undefined;
} else {
ngModel.$setValidity('date', true);
return date;
}
} else {
ngModel.$setValidity('date', false);
return undefined;
}
}
ngModel.$parsers.unshift(parseDate);
// Inner change
scope.dateSelection = function() {
ngModel.$setViewValue(scope.date);
ngModel.$render();
if (closeOnDateSelection) {
setOpen( false );
}
};
element.bind('input change keyup', function() {
scope.$apply(function() {
updateCalendar();
});
});
// Outter change
ngModel.$render = function() {
var date = ngModel.$viewValue ? dateFilter(ngModel.$viewValue, dateFormat) : '';
element.val(date);
updateCalendar();
};
function updateCalendar() {
scope.date = ngModel.$modelValue;
updatePosition();
}
function addWatchableAttribute(attribute, scopeProperty, datepickerAttribute) {
if (attribute) {
originalScope.$watch($parse(attribute), function(value){
scope[scopeProperty] = value;
});
datepickerEl.attr(datepickerAttribute || scopeProperty, scopeProperty);
}
}
addWatchableAttribute(attrs.min, 'min');
addWatchableAttribute(attrs.max, 'max');
if (attrs.showWeeks) {
addWatchableAttribute(attrs.showWeeks, 'showWeeks', 'show-weeks');
} else {
scope.showWeeks = true;
datepickerEl.attr('show-weeks', 'showWeeks');
}
if (attrs.dateDisabled) {
datepickerEl.attr('date-disabled', attrs.dateDisabled);
}
function updatePosition() {
scope.position = $position.position(element);
scope.position.top = scope.position.top + element.prop('offsetHeight');
}
var documentBindingInitialized = false, elementFocusInitialized = false;
scope.$watch('isOpen', function(value) {
if (value) {
updatePosition();
$document.bind('click', documentClickBind);
if(elementFocusInitialized) {
element.unbind('focus', elementFocusBind);
}
element[0].focus();
documentBindingInitialized = true;
} else {
if(documentBindingInitialized) {
$document.unbind('click', documentClickBind);
}
element.bind('focus', elementFocusBind);
elementFocusInitialized = true;
}
if ( setIsOpen ) {
setIsOpen(originalScope, value);
}
});
var $setModelValue = $parse(attrs.ngModel).assign;
scope.today = function() {
$setModelValue(originalScope, new Date());
};
scope.clear = function() {
$setModelValue(originalScope, null);
};
element.after($compile(popupEl)(scope));
}
};
}])
.directive('datepickerPopupWrap', [function() {
return {
restrict:'E',
replace: true,
transclude: true,
templateUrl: 'template/datepicker/popup.html',
link:function (scope, element, attrs) {
element.bind('click', function(event) {
event.preventDefault();
event.stopPropagation();
});
}
};
}]);
angular.module('ui.bootstrap.pagination', [])
.controller('PaginationController', ['$scope', '$attrs', '$parse', '$interpolate', function ($scope, $attrs, $parse, $interpolate) {
var self = this;
this.init = function(defaultItemsPerPage) {
if ($attrs.itemsPerPage) {
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
self.itemsPerPage = parseInt(value, 10);
$scope.totalPages = self.calculateTotalPages();
});
} else {
this.itemsPerPage = defaultItemsPerPage;
}
};
this.noPrevious = function() {
return this.page === 1;
};
this.noNext = function() {
return this.page === $scope.totalPages;
};
this.isActive = function(page) {
return this.page === page;
};
this.calculateTotalPages = function() {
return this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
};
this.getAttributeValue = function(attribute, defaultValue, interpolate) {
return angular.isDefined(attribute) ? (interpolate ? $interpolate(attribute)($scope.$parent) : $scope.$parent.$eval(attribute)) : defaultValue;
};
this.render = function() {
this.page = parseInt($scope.page, 10) || 1;
$scope.pages = this.getPages(this.page, $scope.totalPages);
};
$scope.selectPage = function(page) {
if ( ! self.isActive(page) && page > 0 && page <= $scope.totalPages) {
$scope.page = page;
$scope.onSelectPage({ page: page });
}
};
$scope.$watch('totalItems', function() {
$scope.totalPages = self.calculateTotalPages();
});
$scope.$watch('totalPages', function(value) {
if ( $attrs.numPages ) {
$scope.numPages = value; // Readonly variable
}
if ( self.page > value ) {
$scope.selectPage(value);
} else {
self.render();
}
});
$scope.$watch('page', function() {
self.render();
});
}])
.constant('paginationConfig', {
itemsPerPage: 10,
boundaryLinks: false,
directionLinks: true,
firstText: 'First',
previousText: 'Previous',
nextText: 'Next',
lastText: 'Last',
rotate: true
})
.directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
return {
restrict: 'EA',
scope: {
page: '=',
totalItems: '=',
onSelectPage:' &',
numPages: '='
},
controller: 'PaginationController',
templateUrl: 'template/pagination/pagination.html',
replace: true,
link: function(scope, element, attrs, paginationCtrl) {
// Setup configuration parameters
var maxSize,
boundaryLinks = paginationCtrl.getAttributeValue(attrs.boundaryLinks, config.boundaryLinks ),
directionLinks = paginationCtrl.getAttributeValue(attrs.directionLinks, config.directionLinks ),
firstText = paginationCtrl.getAttributeValue(attrs.firstText, config.firstText, true),
previousText = paginationCtrl.getAttributeValue(attrs.previousText, config.previousText, true),
nextText = paginationCtrl.getAttributeValue(attrs.nextText, config.nextText, true),
lastText = paginationCtrl.getAttributeValue(attrs.lastText, config.lastText, true),
rotate = paginationCtrl.getAttributeValue(attrs.rotate, config.rotate);
paginationCtrl.init(config.itemsPerPage);
if (attrs.maxSize) {
scope.$parent.$watch($parse(attrs.maxSize), function(value) {
maxSize = parseInt(value, 10);
paginationCtrl.render();
});
}
// Create page object used in template
function makePage(number, text, isActive, isDisabled) {
return {
number: number,
text: text,
active: isActive,
disabled: isDisabled
};
}
paginationCtrl.getPages = function(currentPage, totalPages) {
var pages = [];
// Default page limits
var startPage = 1, endPage = totalPages;
var isMaxSized = ( angular.isDefined(maxSize) && maxSize < totalPages );
// recompute if maxSize
if ( isMaxSized ) {
if ( rotate ) {
// Current page is displayed in the middle of the visible ones
startPage = Math.max(currentPage - Math.floor(maxSize/2), 1);
endPage = startPage + maxSize - 1;
// Adjust if limit is exceeded
if (endPage > totalPages) {
endPage = totalPages;
startPage = endPage - maxSize + 1;
}
} else {
// Visible pages are paginated with maxSize
startPage = ((Math.ceil(currentPage / maxSize) - 1) * maxSize) + 1;
// Adjust last page if limit is exceeded
endPage = Math.min(startPage + maxSize - 1, totalPages);
}
}
// Add page number links
for (var number = startPage; number <= endPage; number++) {
var page = makePage(number, number, paginationCtrl.isActive(number), false);
pages.push(page);
}
// Add links to move between page sets
if ( isMaxSized && ! rotate ) {
if ( startPage > 1 ) {
var previousPageSet = makePage(startPage - 1, '...', false, false);
pages.unshift(previousPageSet);
}
if ( endPage < totalPages ) {
var nextPageSet = makePage(endPage + 1, '...', false, false);
pages.push(nextPageSet);
}
}
// Add previous & next links
if (directionLinks) {
var previousPage = makePage(currentPage - 1, previousText, false, paginationCtrl.noPrevious());
pages.unshift(previousPage);
var nextPage = makePage(currentPage + 1, nextText, false, paginationCtrl.noNext());
pages.push(nextPage);
}
// Add first & last links
if (boundaryLinks) {
var firstPage = makePage(1, firstText, false, paginationCtrl.noPrevious());
pages.unshift(firstPage);
var lastPage = makePage(totalPages, lastText, false, paginationCtrl.noNext());
pages.push(lastPage);
}
return pages;
};
}
};
}])
.constant('pagerConfig', {
itemsPerPage: 10,
previousText: '« Previous',
nextText: 'Next »',
align: true
})
.directive('pager', ['pagerConfig', function(config) {
return {
restrict: 'EA',
scope: {
page: '=',
totalItems: '=',
onSelectPage:' &',
numPages: '='
},
controller: 'PaginationController',
templateUrl: 'template/pagination/pager.html',
replace: true,
link: function(scope, element, attrs, paginationCtrl) {
// Setup configuration parameters
var previousText = paginationCtrl.getAttributeValue(attrs.previousText, config.previousText, true),
nextText = paginationCtrl.getAttributeValue(attrs.nextText, config.nextText, true),
align = paginationCtrl.getAttributeValue(attrs.align, config.align);
paginationCtrl.init(config.itemsPerPage);
// Create page object used in template
function makePage(number, text, isDisabled, isPrevious, isNext) {
return {
number: number,
text: text,
disabled: isDisabled,
previous: ( align && isPrevious ),
next: ( align && isNext )
};
}
paginationCtrl.getPages = function(currentPage) {
return [
makePage(currentPage - 1, previousText, paginationCtrl.noPrevious(), true, false),
makePage(currentPage + 1, nextText, paginationCtrl.noNext(), false, true)
];
};
}
};
}]);
angular.module('ui.bootstrap.rating', [])
.constant('ratingConfig', {
max: 5,
stateOn: null,
stateOff: null
})
.controller('RatingController', ['$scope', '$attrs', '$parse', 'ratingConfig', function($scope, $attrs, $parse, ratingConfig) {
this.maxRange = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max;
this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn;
this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff;
this.createDefaultRange = function(len) {
var defaultStateObject = {
stateOn: this.stateOn,
stateOff: this.stateOff
};
var states = new Array(len);
for (var i = 0; i < len; i++) {
states[i] = defaultStateObject;
}
return states;
};
this.normalizeRange = function(states) {
for (var i = 0, n = states.length; i < n; i++) {
states[i].stateOn = states[i].stateOn || this.stateOn;
states[i].stateOff = states[i].stateOff || this.stateOff;
}
return states;
};
// Get objects used in template
$scope.range = angular.isDefined($attrs.ratingStates) ? this.normalizeRange(angular.copy($scope.$parent.$eval($attrs.ratingStates))): this.createDefaultRange(this.maxRange);
$scope.rate = function(value) {
if ( $scope.readonly || $scope.value === value) {
return;
}
$scope.value = value;
};
$scope.enter = function(value) {
if ( ! $scope.readonly ) {
$scope.val = value;
}
$scope.onHover({value: value});
};
$scope.reset = function() {
$scope.val = angular.copy($scope.value);
$scope.onLeave();
};
$scope.$watch('value', function(value) {
$scope.val = value;
});
$scope.readonly = false;
if ($attrs.readonly) {
$scope.$parent.$watch($parse($attrs.readonly), function(value) {
$scope.readonly = !!value;
});
}
}])
.directive('rating', function() {
return {
restrict: 'EA',
scope: {
value: '=',
onHover: '&',
onLeave: '&'
},
controller: 'RatingController',
templateUrl: 'template/rating/rating.html',
replace: true
};
});
angular.module('ui.bootstrap.timepicker', [])
.constant('timepickerConfig', {
hourStep: 1,
minuteStep: 1,
showMeridian: true,
meridians: ['AM', 'PM'],
readonlyInput: false,
mousewheel: true
})
.directive('timepicker', ['$parse', '$log', 'timepickerConfig', function ($parse, $log, timepickerConfig) {
return {
restrict: 'EA',
require:'?^ngModel',
replace: true,
scope: {},
templateUrl: 'template/timepicker/timepicker.html',
link: function(scope, element, attrs, ngModel) {
if ( !ngModel ) {
return; // do nothing if no ng-model
}
var selected = new Date(), meridians = timepickerConfig.meridians;
var hourStep = timepickerConfig.hourStep;
if (attrs.hourStep) {
scope.$parent.$watch($parse(attrs.hourStep), function(value) {
hourStep = parseInt(value, 10);
});
}
var minuteStep = timepickerConfig.minuteStep;
if (attrs.minuteStep) {
scope.$parent.$watch($parse(attrs.minuteStep), function(value) {
minuteStep = parseInt(value, 10);
});
}
// 12H / 24H mode
scope.showMeridian = timepickerConfig.showMeridian;
if (attrs.showMeridian) {
scope.$parent.$watch($parse(attrs.showMeridian), function(value) {
scope.showMeridian = !!value;
if ( ngModel.$error.time ) {
// Evaluate from template
var hours = getHoursFromTemplate(), minutes = getMinutesFromTemplate();
if (angular.isDefined( hours ) && angular.isDefined( minutes )) {
selected.setHours( hours );
refresh();
}
} else {
updateTemplate();
}
});
}
// Get scope.hours in 24H mode if valid
function getHoursFromTemplate ( ) {
var hours = parseInt( scope.hours, 10 );
var valid = ( scope.showMeridian ) ? (hours > 0 && hours < 13) : (hours >= 0 && hours < 24);
if ( !valid ) {
return undefined;
}
if ( scope.showMeridian ) {
if ( hours === 12 ) {
hours = 0;
}
if ( scope.meridian === meridians[1] ) {
hours = hours + 12;
}
}
return hours;
}
function getMinutesFromTemplate() {
var minutes = parseInt(scope.minutes, 10);
return ( minutes >= 0 && minutes < 60 ) ? minutes : undefined;
}
function pad( value ) {
return ( angular.isDefined(value) && value.toString().length < 2 ) ? '0' + value : value;
}
// Input elements
var inputs = element.find('input'), hoursInputEl = inputs.eq(0), minutesInputEl = inputs.eq(1);
// Respond on mousewheel spin
var mousewheel = (angular.isDefined(attrs.mousewheel)) ? scope.$eval(attrs.mousewheel) : timepickerConfig.mousewheel;
if ( mousewheel ) {
var isScrollingUp = function(e) {
if (e.originalEvent) {
e = e.originalEvent;
}
//pick correct delta variable depending on event
var delta = (e.wheelDelta) ? e.wheelDelta : -e.deltaY;
return (e.detail || delta > 0);
};
hoursInputEl.bind('mousewheel wheel', function(e) {
scope.$apply( (isScrollingUp(e)) ? scope.incrementHours() : scope.decrementHours() );
e.preventDefault();
});
minutesInputEl.bind('mousewheel wheel', function(e) {
scope.$apply( (isScrollingUp(e)) ? scope.incrementMinutes() : scope.decrementMinutes() );
e.preventDefault();
});
}
scope.readonlyInput = (angular.isDefined(attrs.readonlyInput)) ? scope.$eval(attrs.readonlyInput) : timepickerConfig.readonlyInput;
if ( ! scope.readonlyInput ) {
var invalidate = function(invalidHours, invalidMinutes) {
ngModel.$setViewValue( null );
ngModel.$setValidity('time', false);
if (angular.isDefined(invalidHours)) {
scope.invalidHours = invalidHours;
}
if (angular.isDefined(invalidMinutes)) {
scope.invalidMinutes = invalidMinutes;
}
};
scope.updateHours = function() {
var hours = getHoursFromTemplate();
if ( angular.isDefined(hours) ) {
selected.setHours( hours );
refresh( 'h' );
} else {
invalidate(true);
}
};
hoursInputEl.bind('blur', function(e) {
if ( !scope.validHours && scope.hours < 10) {
scope.$apply( function() {
scope.hours = pad( scope.hours );
});
}
});
scope.updateMinutes = function() {
var minutes = getMinutesFromTemplate();
if ( angular.isDefined(minutes) ) {
selected.setMinutes( minutes );
refresh( 'm' );
} else {
invalidate(undefined, true);
}
};
minutesInputEl.bind('blur', function(e) {
if ( !scope.invalidMinutes && scope.minutes < 10 ) {
scope.$apply( function() {
scope.minutes = pad( scope.minutes );
});
}
});
} else {
scope.updateHours = angular.noop;
scope.updateMinutes = angular.noop;
}
ngModel.$render = function() {
var date = ngModel.$modelValue ? new Date( ngModel.$modelValue ) : null;
if ( isNaN(date) ) {
ngModel.$setValidity('time', false);
$log.error('Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.');
} else {
if ( date ) {
selected = date;
}
makeValid();
updateTemplate();
}
};
// Call internally when we know that model is valid.
function refresh( keyboardChange ) {
makeValid();
ngModel.$setViewValue( new Date(selected) );
updateTemplate( keyboardChange );
}
function makeValid() {
ngModel.$setValidity('time', true);
scope.invalidHours = false;
scope.invalidMinutes = false;
}
function updateTemplate( keyboardChange ) {
var hours = selected.getHours(), minutes = selected.getMinutes();
if ( scope.showMeridian ) {
hours = ( hours === 0 || hours === 12 ) ? 12 : hours % 12; // Convert 24 to 12 hour system
}
scope.hours = keyboardChange === 'h' ? hours : pad(hours);
scope.minutes = keyboardChange === 'm' ? minutes : pad(minutes);
scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
}
function addMinutes( minutes ) {
var dt = new Date( selected.getTime() + minutes * 60000 );
selected.setHours( dt.getHours(), dt.getMinutes() );
refresh();
}
scope.incrementHours = function() {
addMinutes( hourStep * 60 );
};
scope.decrementHours = function() {
addMinutes( - hourStep * 60 );
};
scope.incrementMinutes = function() {
addMinutes( minuteStep );
};
scope.decrementMinutes = function() {
addMinutes( - minuteStep );
};
scope.toggleMeridian = function() {
addMinutes( 12 * 60 * (( selected.getHours() < 12 ) ? 1 : -1) );
};
}
};
}]);
angular.module('ui.bootstrap.bindHtml', [])
.directive('bindHtmlUnsafe', function () {
return function (scope, element, attr) {
element.addClass('ng-binding').data('$binding', attr.bindHtmlUnsafe);
scope.$watch(attr.bindHtmlUnsafe, function bindHtmlUnsafeWatchAction(value) {
element.html(value || '');
});
};
});
angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap.bindHtml'])
/**
* A helper service that can parse typeahead's syntax (string provided by users)
* Extracted to a separate service for ease of unit testing
*/
.factory('typeaheadParser', ['$parse', function ($parse) {
// 00000111000000000000022200000000000000003333333333333330000000000044000
var TYPEAHEAD_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?\s+for\s+(?:([\$\w][\$\w\d]*))\s+in\s+(.*)$/;
return {
parse:function (input) {
var match = input.match(TYPEAHEAD_REGEXP), modelMapper, viewMapper, source;
if (!match) {
throw new Error(
"Expected typeahead specification in form of '_modelValue_ (as _label_)? for _item_ in _collection_'" +
" but got '" + input + "'.");
}
return {
itemName:match[3],
source:$parse(match[4]),
viewMapper:$parse(match[2] || match[1]),
modelMapper:$parse(match[1])
};
}
};
}])
.directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser',
function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) {
var HOT_KEYS = [9, 13, 27, 38, 40];
return {
require:'ngModel',
link:function (originalScope, element, attrs, modelCtrl) {
//SUPPORTED ATTRIBUTES (OPTIONS)
//minimal no of characters that needs to be entered before typeahead kicks-in
var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 1;
//minimal wait time after last character typed before typehead kicks-in
var waitTime = originalScope.$eval(attrs.typeaheadWaitMs) || 0;
//should it restrict model values to the ones selected from the popup only?
var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false;
//binding to a variable that indicates if matches are being retrieved asynchronously
var isLoadingSetter = $parse(attrs.typeaheadLoading).assign || angular.noop;
//a callback executed when a match is selected
var onSelectCallback = $parse(attrs.typeaheadOnSelect);
var inputFormatter = attrs.typeaheadInputFormatter ? $parse(attrs.typeaheadInputFormatter) : undefined;
//INTERNAL VARIABLES
//model setter executed upon match selection
var $setModelValue = $parse(attrs.ngModel).assign;
//expressions used by typeahead
var parserResult = typeaheadParser.parse(attrs.typeahead);
//pop-up element used to display matches
var popUpEl = angular.element('<typeahead-popup></typeahead-popup>');
popUpEl.attr({
matches: 'matches',
active: 'activeIdx',
select: 'select(activeIdx)',
query: 'query',
position: 'position'
});
//custom item template
if (angular.isDefined(attrs.typeaheadTemplateUrl)) {
popUpEl.attr('template-url', attrs.typeaheadTemplateUrl);
}
//create a child scope for the typeahead directive so we are not polluting original scope
//with typeahead-specific data (matches, query etc.)
var scope = originalScope.$new();
originalScope.$on('$destroy', function(){
scope.$destroy();
});
var resetMatches = function() {
scope.matches = [];
scope.activeIdx = -1;
};
var getMatchesAsync = function(inputValue) {
var locals = {$viewValue: inputValue};
isLoadingSetter(originalScope, true);
$q.when(parserResult.source(scope, locals)).then(function(matches) {
//it might happen that several async queries were in progress if a user were typing fast
//but we are interested only in responses that correspond to the current view value
if (inputValue === modelCtrl.$viewValue) {
if (matches.length > 0) {
scope.activeIdx = 0;
scope.matches.length = 0;
//transform labels
for(var i=0; i<matches.length; i++) {
locals[parserResult.itemName] = matches[i];
scope.matches.push({
label: parserResult.viewMapper(scope, locals),
model: matches[i]
});
}
scope.query = inputValue;
//position pop-up with matches - we need to re-calculate its position each time we are opening a window
//with matches as a pop-up might be absolute-positioned and position of an input might have changed on a page
//due to other elements being rendered
scope.position = $position.position(element);
scope.position.top = scope.position.top + element.prop('offsetHeight');
} else {
resetMatches();
}
isLoadingSetter(originalScope, false);
}
}, function(){
resetMatches();
isLoadingSetter(originalScope, false);
});
};
resetMatches();
//we need to propagate user's query so we can higlight matches
scope.query = undefined;
//Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later
var timeoutPromise;
//plug into $parsers pipeline to open a typeahead on view changes initiated from DOM
//$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
modelCtrl.$parsers.unshift(function (inputValue) {
resetMatches();
if (inputValue && inputValue.length >= minSearch) {
if (waitTime > 0) {
if (timeoutPromise) {
$timeout.cancel(timeoutPromise);//cancel previous timeout
}
timeoutPromise = $timeout(function () {
getMatchesAsync(inputValue);
}, waitTime);
} else {
getMatchesAsync(inputValue);
}
}
if (isEditable) {
return inputValue;
} else {
modelCtrl.$setValidity('editable', false);
return undefined;
}
});
modelCtrl.$formatters.push(function (modelValue) {
var candidateViewValue, emptyViewValue;
var locals = {};
if (inputFormatter) {
locals['$model'] = modelValue;
return inputFormatter(originalScope, locals);
} else {
//it might happen that we don't have enough info to properly render input value
//we need to check for this situation and simply return model value if we can't apply custom formatting
locals[parserResult.itemName] = modelValue;
candidateViewValue = parserResult.viewMapper(originalScope, locals);
locals[parserResult.itemName] = undefined;
emptyViewValue = parserResult.viewMapper(originalScope, locals);
return candidateViewValue!== emptyViewValue ? candidateViewValue : modelValue;
}
});
scope.select = function (activeIdx) {
//called from within the $digest() cycle
var locals = {};
var model, item;
locals[parserResult.itemName] = item = scope.matches[activeIdx].model;
model = parserResult.modelMapper(originalScope, locals);
$setModelValue(originalScope, model);
modelCtrl.$setValidity('editable', true);
onSelectCallback(originalScope, {
$item: item,
$model: model,
$label: parserResult.viewMapper(originalScope, locals)
});
resetMatches();
//return focus to the input element if a mach was selected via a mouse click event
element[0].focus();
};
//bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)
element.bind('keydown', function (evt) {
//typeahead is open and an "interesting" key was pressed
if (scope.matches.length === 0 || HOT_KEYS.indexOf(evt.which) === -1) {
return;
}
evt.preventDefault();
if (evt.which === 40) {
scope.activeIdx = (scope.activeIdx + 1) % scope.matches.length;
scope.$digest();
} else if (evt.which === 38) {
scope.activeIdx = (scope.activeIdx ? scope.activeIdx : scope.matches.length) - 1;
scope.$digest();
} else if (evt.which === 13 || evt.which === 9) {
scope.$apply(function () {
scope.select(scope.activeIdx);
});
} else if (evt.which === 27) {
evt.stopPropagation();
resetMatches();
scope.$digest();
}
});
// Keep reference to click handler to unbind it.
var dismissClickHandler = function (evt) {
if (element[0] !== evt.target) {
resetMatches();
scope.$digest();
}
};
$document.bind('click', dismissClickHandler);
originalScope.$on('$destroy', function(){
$document.unbind('click', dismissClickHandler);
});
element.after($compile(popUpEl)(scope));
}
};
}])
.directive('typeaheadPopup', function () {
return {
restrict:'E',
scope:{
matches:'=',
query:'=',
active:'=',
position:'=',
select:'&'
},
replace:true,
// Hardcode this path until this is fixed: https://github.com/decipherinc/angular-tags/issues/16#issuecomment-26614629
templateUrl:'/ui-bootstrap/template/typeahead/typeahead-popup.html',
link:function (scope, element, attrs) {
scope.templateUrl = attrs.templateUrl;
scope.isOpen = function () {
return scope.matches.length > 0;
};
scope.isActive = function (matchIdx) {
return scope.active == matchIdx;
};
scope.selectActive = function (matchIdx) {
scope.active = matchIdx;
};
scope.selectMatch = function (activeIdx) {
scope.select({activeIdx:activeIdx});
};
}
};
})
.directive('typeaheadMatch', ['$http', '$templateCache', '$compile', '$parse', function ($http, $templateCache, $compile, $parse) {
return {
restrict:'E',
scope:{
index:'=',
match:'=',
query:'='
},
link:function (scope, element, attrs) {
var tplUrl = $parse(attrs.templateUrl)(scope.$parent) || 'template/typeahead/typeahead-match.html';
$http.get(tplUrl, {cache: $templateCache}).success(function(tplContent){
element.replaceWith($compile(tplContent.trim())(scope));
});
}
};
}])
.filter('typeaheadHighlight', function() {
function escapeRegexp(queryToEscape) {
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
return function(matchItem, query) {
return query ? matchItem.replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem;
};
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 2 | /*global angular, CodeMirror, Error*/
/**
* Binds a CodeMirror widget to a <textarea> element.
*/
angular.module('ui.codemirror', [])
.constant('uiCodemirrorConfig', {})
.directive('uiCodemirror', ['uiCodemirrorConfig', '$timeout', function (uiCodemirrorConfig, $timeout) {
'use strict';
var events = ["cursorActivity", "viewportChange", "gutterClick", "focus", "blur", "scroll", "update"];
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attrs, ngModel) {
var options, opts, onChange, deferCodeMirror, codeMirror;
if (elm[0].type !== 'textarea') {
throw new Error('uiCodemirror3 can only be applied to a textarea element');
}
options = uiCodemirrorConfig.codemirror || {};
opts = angular.extend({}, options, scope.$eval(attrs.uiCodemirror));
onChange = function (aEvent) {
return function (instance, changeObj) {
var newValue = instance.getValue();
if (newValue !== ngModel.$viewValue) {
ngModel.$setViewValue(newValue);
}
if (typeof aEvent === "function") {
aEvent(instance, changeObj);
}
if (!scope.$$phase) {
scope.$apply();
}
};
};
deferCodeMirror = function () {
codeMirror = CodeMirror.fromTextArea(elm[0], opts);
// Refresh codemirror externally this way...
//$('[ui-codemirror]').trigger('refresh')
elm.on('refresh', function () {
codeMirror.refresh()
});
if (angular.isDefined(scope[attrs.uiCodemirror])) {
scope.$watch(attrs.uiCodemirror, function (newValues) {
for (var key in newValues) {
if (newValues.hasOwnProperty(key)) {
codeMirror.setOption(key, newValues[key]);
}
}
}, true);
}
codeMirror.on("change", onChange(opts.onChange));
for (var i = 0, n = events.length, aEvent; i < n; ++i) {
aEvent = opts["on" + events[i].charAt(0).toUpperCase() + events[i].slice(1)];
if (aEvent === void 0) {
continue;
}
if (typeof aEvent !== "function") {
continue;
}
codeMirror.on(events[i], aEvent);
}
// CodeMirror expects a string, so make sure it gets one.
// This does not change the model.
ngModel.$formatters.push(function (value) {
if (angular.isUndefined(value) || value === null) {
return '';
}
else if (angular.isObject(value) || angular.isArray(value)) {
throw new Error('ui-codemirror cannot use an object or an array as a model');
}
return value;
});
// Override the ngModelController $render method, which is what gets called when the model is updated.
// This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
ngModel.$render = function () {
codeMirror.setValue(ngModel.$viewValue);
};
if (!ngModel.$viewValue){
ngModel.$setViewValue(elm.text());
ngModel.$render();
}
// Watch ui-refresh and refresh the directive
if (attrs.uiRefresh) {
scope.$watch(attrs.uiRefresh, function (newVal, oldVal) {
// Skip the initial watch firing
if (newVal !== oldVal) {
$timeout(function () {
codeMirror.refresh();
});
}
});
}
// onLoad callback
if (angular.isFunction(opts.onLoad)) {
opts.onLoad(codeMirror);
}
};
$timeout(deferCodeMirror);
}
};
}]);
|